Skip to content

Instantly share code, notes, and snippets.

@dhermes
Last active October 18, 2019 15:33
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/4998576c09a1c55e184b97c61c74b080 to your computer and use it in GitHub Desktop.
Save dhermes/4998576c09a1c55e184b97c61c74b080 to your computer and use it in GitHub Desktop.
Benchmarking `_.each` vs. For Loop
node_modules/

Benchmarking _.each vs. For Loop

Running the microbenchmark produces

$ npm run benchmark

> @ benchmark .../lodash-each-microbenchmark
> node index.js

withForLoop x 2,291,112 ops/sec ±1.78% (87 runs sampled)
withLodash x 732,795 ops/sec ±1.34% (85 runs sampled)

The comparison is between just a vanilla for loop

for (const value of VALUES) {
  result.push(value * value);
}

and using _.each

_.each(VALUES, value => {
  result.push(value * value);
});

See also a jsPerf with a similar test.

const Benchmark = require("benchmark");
const _ = require("lodash");
const VALUES = [
46,
-4,
17,
-49,
-20,
-23,
4,
41,
25,
50,
-42,
-49,
-30,
-18,
-7,
-39,
43,
-27,
48,
-32,
44,
33,
-28,
46,
-25,
4,
23,
-40,
-24,
-5,
46,
45,
36,
-44,
38,
-24,
29,
-32,
35,
40,
-24,
27,
-25,
6,
1,
39,
-33,
8,
30,
-15,
-35,
15,
45,
-24,
48,
23,
-37,
1,
-24,
41,
7,
28,
23,
15,
13,
18,
-42,
37,
7,
28,
-25,
31,
-48,
-47,
-9,
42,
-32,
-20,
-45,
47,
17,
-41,
21,
-16,
-40,
-24,
-16,
22,
-1,
27,
16,
18,
39,
3,
-39,
-1,
-38,
5,
15,
3,
];
function withForLoop() {
const result = [];
for (const value of VALUES) {
result.push(value * value);
}
return result.length === VALUES.length;
}
function withLodash() {
const result = [];
_.each(VALUES, value => {
result.push(value * value);
});
return result.length === VALUES.length;
}
function main() {
const suite = new Benchmark.Suite();
suite
.add("withForLoop", withForLoop)
.add("withLodash", withLodash)
.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=="
},
"prettier": {
"version": "1.18.2",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-1.18.2.tgz",
"integrity": "sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw=="
}
}
}
{
"scripts": {
"benchmark": "node index.js",
"prettier": "prettier index.js --write"
},
"prettier": {
"parser": "typescript",
"printWidth": 120,
"quoteProps": "consistent",
"semi": true,
"singleQuote": false,
"trailingComma": "all"
},
"dependencies": {
"benchmark": "2.1.4",
"lodash": "4.17.15",
"prettier": "1.18.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment