Skip to content

Instantly share code, notes, and snippets.

@edysegura
Created April 18, 2016 17:03
Show Gist options
  • Save edysegura/ac3b371335dd50b60142254d186581eb to your computer and use it in GitHub Desktop.
Save edysegura/ac3b371335dd50b60142254d186581eb to your computer and use it in GitHub Desktop.
[JS] forEach benchmark
var arr = [];
for (var i = 0; i < 100000; i++) arr[i] = i;
var numberOfRuns = 100;
function runTest(name, f) {
var totalTime = 0;
console.time(name);
for (var r = 0; r < numberOfRuns; r++) {
f();
}
return console.timeEnd(name);
}
function testFunction(v) {
v;
}
var forTime = runTest('for', function() {
for (var j = 0; j < arr.length; j++) {
testFunction(arr[j]);
}
});
var forEachTime = runTest('forEach', function() {
arr.forEach(testFunction);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment