Created
September 23, 2020 19:51
-
-
Save TwitchBronBron/16f3b20f76c66527169a61f5c2e1d0ce to your computer and use it in GitHub Desktop.
benchmark various for loop implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
var i, | |
value, | |
length, | |
valuesTen = [], | |
valuesHundred = [], | |
valuesThousand = [], | |
valuesTenThousand = [], | |
sum = 0; | |
for (i = 0; i < 10000; i++) { | |
if (i < 10) { | |
valuesTen.push(i); | |
} | |
if (i < 100) { | |
valuesHundred.push(i); | |
} | |
if (i < 1000) { | |
valuesThousand.push(i); | |
} | |
if (i < 10000) { | |
valuesTenThousand.push(i); | |
} | |
} | |
function add(val) { | |
sum += val; | |
} | |
}; | |
suite.add("values.forEach(add); 10", function () { | |
valuesTen.forEach(add); | |
}); | |
suite.add("values.forEach(add); 100", function () { | |
valuesHundred.forEach(add); | |
}); | |
suite.add("values.forEach(add); 1,000", function () { | |
valuesThousand.forEach(add); | |
}); | |
suite.add("values.forEach(add); 1,0000", function () { | |
valuesTenThousand.forEach(add); | |
}); | |
suite.add("for (i = 0; i < values.length; i++) 10 {", function () { | |
for (i = 0; i < valuesTen.length; i++) { | |
sum += valuesTen[i]; | |
} | |
}); | |
suite.add("for (i = 0; i < values.length; i++) 100 {", function () { | |
for (i = 0; i < valuesHundred.length; i++) { | |
sum += valuesHundred[i]; | |
} | |
}); | |
suite.add("for (i = 0; i < values.length; i++) 1,000 {", function () { | |
for (i = 0; i < valuesThousand.length; i++) { | |
sum += valuesThousand[i]; | |
} | |
}); | |
suite.add("for (i = 0; i < values.length; i++) 10,000 {", function () { | |
for (i = 0; i < valuesTenThousand.length; i++) { | |
sum += valuesTenThousand[i]; | |
} | |
}); | |
suite.add("for (value of values) 10 {", function () { | |
for (value of valuesTen) { | |
sum += value; | |
} | |
}); | |
suite.add("for (value of values) 100 {", function () { | |
for (value of valuesHundred) { | |
sum += value; | |
} | |
}); | |
suite.add("for (value of values) 1,000 {", function () { | |
for (value of valuesThousand) { | |
sum += value; | |
} | |
}); | |
suite.add("for (value of values) 10,000 {", function () { | |
for (value of valuesTenThousand) { | |
sum += value; | |
} | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on('error', function (...args) { | |
console.log(...args); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item, idx) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("for-of loop vs forEach"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This benchmarks various array sizes and different loop types. Thanks to this gist for the baseline of this test.
Here are the results running against node 10, 12, and 14 on my windows 10 device. In all cases but one back on node 10, the
for(var i
loop is fastest, followed closely byfor of
, then trailing behind isArray.forEach
.