forEach vs reduce vs for loop

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var testData = [];
  
  for (var i = 0; i < 100; i++) {
    testData.push(i);
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
forEach
var res = 0;

testData.forEach(function(x) {
  res += x;
});
pending…
reduce
var res = testData.reduce(function(sum, x) {
  return sum + x;
}, 0);
pending…
for loop optimized
var res = 0;

for (var i = 0, len = testData.length; i < len; i++) {
  res += testData[i];
}
pending…
for loop
var res = 0;

for (var i = 0; i < testData.length; i++) {
  res += testData[i];
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments