Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Created April 29, 2020 20:56
Show Gist options
  • Save justinvdm/4942d278e438152e5b236958e7af3752 to your computer and use it in GitHub Desktop.
Save justinvdm/4942d278e438152e5b236958e7af3752 to your computer and use it in GitHub Desktop.
const Benchmark = require("benchmark");
displayIntro()
const N = 1000;
const A = 'a'
const B = 'b'
const C = 'c'
new Benchmark.Suite("array-join-vs-string", createConf())
.add("array join", () => {
let i = -1;
while (++i < N) [A, B, C].join(' ');
})
.add("str concat", () => {
let i = -1;
while (++i < N) A + ' ' + B + ' ' + C
})
.add("template literal", () => {
let i = -1;
while (++i < N) `${A} ${B} ${C}`
})
.run();
function createConf() {
return {
onComplete(e) {
this.map((bench) =>
console.log(
JSON.stringify(
{
name: bench.name,
hz: bench.hz.toFixed(2),
relativeMarginOfError: `${bench.stats.rme.toFixed(2)}%`,
runs: bench.stats.sample.length,
}
)
)
);
},
onError(e) {
console.error(e.target.error);
}
}
}
function displayIntro() {
console.log(JSON.stringify({ nodeVersion: process.version }))
}
{"nodeVersion":"v12.16.2"}
{"name":"array join","hz":"5093.61","relativeMarginOfError":"0.82%","runs":90}
{"name":"str concat","hz":"1123945.33","relativeMarginOfError":"0.63%","runs":90}
{"name":"template literal","hz":"1117304.79","relativeMarginOfError":"1.68%","runs":92}
{
"name": "bench-array-join-vs-string-concat",
"version": "1.0.0",
"description": "",
"main": "_bench-array-join-vs-string-concat.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"bench": "node _bench-array-join-vs-string-concat.js | tee _results.ldjson"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"benchmark": "^2.1.4"
}
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
benchmark@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
integrity sha1-CfPeMckWQl1JjMLuVloOvzwqVik=
dependencies:
lodash "^4.17.4"
platform "^1.3.3"
lodash@^4.17.4:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
platform@^1.3.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
integrity sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment