Skip to content

Instantly share code, notes, and snippets.

@ptn
Created November 27, 2014 00:38
Show Gist options
  • Save ptn/b67e6eb64051d35321e7 to your computer and use it in GitHub Desktop.
Save ptn/b67e6eb64051d35321e7 to your computer and use it in GitHub Desktop.
forEach benchmark
var x = [];
// one million elements
for (var i = 0; i < 1000000; i++) {
x.push(i);
}
function testFor(x) {
console.time('benchmark');
for (var i = 0; i < x.length; i++) {
x[i];
}
console.timeEnd('benchmark');
};
function testForEach(x) {
console.time('benchmark');
x.forEach(function(el) {
el;
});
console.timeEnd('benchmark');
};
testFor(x);
benchmark: 1.484ms
testFor(x);
benchmark: 1.644ms
testFor(x);
benchmark: 1.481ms
testFor(x);
benchmark: 1.478ms
testFor(x);
benchmark: 1.488ms
testForEach(x);
benchmark: 63.130ms
testForEach(x);
benchmark: 60.199ms
testForEach(x);
benchmark: 67.509ms
testForEach(x);
benchmark: 57.232ms
testForEach(x);
benchmark: 55.450ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment