Skip to content

Instantly share code, notes, and snippets.

@dhermes
Last active October 8, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhermes/1c7f3ef52db92d8b8f7fc03ecae4bbf5 to your computer and use it in GitHub Desktop.
Save dhermes/1c7f3ef52db92d8b8f7fc03ecae4bbf5 to your computer and use it in GitHub Desktop.
Benchmarking `_.compact` vs. Branching
node_modules/

Benchmarking _.compact vs. Branching

Running the microbenchmark produces

$ npm run benchmark

> @ benchmark ...
> node index.js

withBranching x 27,485,424 ops/sec ±1.68% (77 runs sampled)
withCompact x 19,542,005 ops/sec ±1.28% (77 runs sampled)

The comparison is between just branching

const result = [];
if (OPTIONS.u) { result.push({ u: 10 }); }
if (OPTIONS.v) { result.push({ v: 11 }); }
if (OPTIONS.w) { result.push({ w: 12 }); }
if (OPTIONS.x) { result.push({ x: 13 }); }
if (OPTIONS.y) { result.push({ y: 14 }); }
if (OPTIONS.z) { result.push({ z: 15 }); }

and using _.compact on an array with potentially false-y entries

const result = _.compact([
  OPTIONS.u && { u: 10 },
  OPTIONS.v && { v: 11 },
  OPTIONS.w && { w: 12 },
  OPTIONS.x && { x: 13 },
  OPTIONS.y && { y: 14 },
  OPTIONS.z && { z: 15 }
]);
const Benchmark = require("benchmark");
const _ = require("lodash");
const OPTIONS = {
u: true,
x: true,
y: true,
};
function withBranching() {
const result = [];
if (OPTIONS.u) { result.push({ u: 10 }); }
if (OPTIONS.v) { result.push({ v: 11 }); }
if (OPTIONS.w) { result.push({ w: 12 }); }
if (OPTIONS.x) { result.push({ x: 13 }); }
if (OPTIONS.y) { result.push({ y: 14 }); }
if (OPTIONS.z) { result.push({ z: 15 }); }
return result.length === 3;
}
function withCompact() {
const result = _.compact([
OPTIONS.u && { u: 10 },
OPTIONS.v && { v: 11 },
OPTIONS.w && { w: 12 },
OPTIONS.x && { x: 13 },
OPTIONS.y && { y: 14 },
OPTIONS.z && { z: 15 },
]);
return result.length === 3;
}
function main() {
const suite = new Benchmark.Suite();
suite
.add("withBranching", withBranching)
.add("withCompact", withCompact)
.on("cycle", function(event) {
console.log(String(event.target));
})
.run({ async: true });
}
if (require.main === module) {
main();
}
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"benchmark": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/benchmark/-/benchmark-2.1.4.tgz",
"integrity": "sha1-CfPeMckWQl1JjMLuVloOvzwqVik=",
"requires": {
"lodash": "^4.17.4",
"platform": "^1.3.3"
}
},
"lodash": {
"version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
},
"platform": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/platform/-/platform-1.3.5.tgz",
"integrity": "sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q=="
}
}
}
{
"scripts": {
"benchmark": "node index.js"
},
"dependencies": {
"benchmark": "2.1.4",
"lodash": "4.17.15"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment