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 }
]);