for vs forEach

JavaScript performance comparison

Revision 284 of this test case created by Nathan

Preparation code

<script>
var values = new Array(10000);
var i, sum;

for (i = 0; i < 10000; i++) {
  values[i] = i % 3;
}
  
function add(val) {

  'use strict';
  sum += val;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  sum = 0;

};
</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
values.forEach(add);
pending…
for loop
for (i = 0; i < values.length; i++) {
  add(values[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