Skip to content

Instantly share code, notes, and snippets.

@heimskr
Created February 8, 2019 12:09
Show Gist options
  • Save heimskr/dfab518975f2b3f4b69711defa1b040e to your computer and use it in GitHub Desktop.
Save heimskr/dfab518975f2b3f4b69711defa1b040e to your computer and use it in GitHub Desktop.
loopperf
const _ = require("lodash");
const n = 10000, m = 10000;
const T = s => console.time(t = s);
const E = s => console.timeEnd(t);
console.log("Iterations:", n);
console.log("Subiterations:", m);
console.log();
let x, t, i, j;
const arrs = [];
for (i = 0; i < n; i++) {
const arr = [];
for (j = 0; j < m; j++)
arr.push(j);
arrs.push(arr);
}
T("for ... of");
x = 0;
for (const arr of arrs) {
for (const v of arr)
x += v;
}
E();
console.log();
T("forEach");
x = 0;
arrs.forEach(arr => arr.forEach(v => x += v));
E();
T("forEach (no return values)");
x = 0;
arrs.forEach(arr => { arr.forEach(v => { x += v }) });
E();
console.log();
T("_.forEach");
x = 0;
_.forEach(arrs, arr => _.forEach(arr, v => x += v));
E();
T("_.forEach (no return values)");
x = 0;
_.forEach(arrs, arr => { _.forEach(arr, v => { x += v }) });
E();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment