Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Last active January 20, 2020 15:05
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 drmikecrowe/86ee5f8b19991bb27fd00f941280e8af to your computer and use it in GitHub Desktop.
Save drmikecrowe/86ee5f8b19991bb27fd00f941280e8af to your computer and use it in GitHub Desktop.
Time various loop structures

Base code/idea comes from this blog post

The output:

Testing vanilla:      4.1ms
Testing lodash:      25.4ms -- 518% slower
Testing es6-for-of:   7.3ms --  78% slower
Testing forEach:      6.1ms --  48% slower
Testing map:          9.2ms -- 125% slower
Testing reverse:      3.4ms --  17% faster
Testing shift:       20.9ms -- 408% slower
const _ = require("lodash");
const perfy = require("perfy");
const times = {};
let baseline = 25000;
let total = 0;
const LOOPS = 100000;
function testVanilla() {
let tmp = 0;
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let p = 0; p < LOOPS; p++) {
for (let i = 0; i < someNumbers.length; i++) {
tmp += someNumbers[i];
}
}
total = tmp; // Set the total to check all other loops against
console.assert(tmp === total);
}
function testLodash() {
let tmp = 0;
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let p = 0; p < LOOPS; p++) {
_.each(someNumbers, function(value) {
tmp += value;
});
}
console.assert(tmp === total);
}
function testES6ForOf() {
let tmp = 0;
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let p = 0; p < LOOPS; p++) {
for (let value of someNumbers) {
tmp += value;
}
}
console.assert(tmp === total);
}
function testForEach() {
let tmp = 0;
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let p = 0; p < LOOPS; p++) {
someNumbers.forEach(value => (tmp += value));
}
console.assert(tmp === total);
}
function testMap() {
let tmp = 0;
for (let p = 0; p < LOOPS; p++) {
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
someNumbers.map(value => (tmp += value));
}
console.assert(tmp === total);
}
function testReverseLoop() {
let tmp = 0;
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let p = 0; p < LOOPS; p++) {
for (let i = someNumbers.length - 1; i >= 0; i--) {
tmp += someNumbers[i];
}
}
console.assert(tmp === total);
}
function testArrayShift() {
let tmp = 0;
for (let p = 0; p < LOOPS; p++) {
const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
while (someNumbers.length) {
tmp += someNumbers.shift();
}
}
console.assert(tmp === total);
}
function doTest(label, fn) {
perfy.start(label);
fn();
let result = perfy.end(label);
times[label] = result.fullMilliseconds;
}
doTest("vanilla", testVanilla);
doTest("lodash", testLodash);
doTest("es6-for-of", testES6ForOf);
doTest("forEach", testForEach);
doTest("map", testMap);
doTest("reverse", testReverseLoop);
doTest("shift", testArrayShift);
baseline = times["vanilla"];
for (let label in times) {
const delta = Math.round(100 * ((times[label] - baseline) / baseline));
const space2 = Math.abs(delta) >= 100 ? "" : " ";
const space1 = times[label] >= 10 ? "" : " ";
const analysis = delta > 0 ? ` -- ${space2}${delta}% slower` : delta < 0 ? ` -- ${space2}${-delta}% faster` : "";
const display = _.padEnd(`Testing ${label}:`, 20);
console.log(`${display} ${space1}${times[label].toFixed(1)}ms${analysis}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment