forEach vs loop

JavaScript performance comparison

Revision 123 of this test case created by jonbo and last updated

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var small_arr = new Array(64);
  for (var i = 0; i < 64; i++) small_arr[i] = (i%8) + 1;
  
  var medium_arr = new Array(4096);
  for (var i = 0; i < 4096; i++) medium_arr[i] = (i%8) + 1;
  
  var large_arr = new Array(65536);
  for (var i = 0; i < 65536; i++) large_arr[i] = (i%8) + 1;
  
  var sum = 0;
  function add(val) {
    sum += val;
  }

};
</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
small forEach
small_arr.forEach(add);
pending…
small loop (inline)
for (var i = 0, l = small_arr.length; i < l; i++) {
  sum += small_arr[i];
}
pending…
small loop
for (var i = 0, l = small_arr.length; i < l; i++) {
  add(small_arr[i]);
}
pending…
med forEach
medium_arr.forEach(add);
pending…
med loop (inline)
for (var i = 0, l = medium_arr.length; i < l; i++) {
  sum += medium_arr[i];
}
pending…
med loop
for (var i = 0, l = medium_arr.length; i < l; i++) {
  add(medium_arr[i]);
}
pending…
large forEach
large_arr.forEach(add);
pending…
large loop (inline)
for (var i = 0, l = large_arr.length; i < l; i++) {
  sum += large_arr[i];
}
pending…
large loop
for (var i = 0, l = large_arr.length; i < l; i++) {
  add(large_arr[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