forEach vs loop

JavaScript performance comparison

Revision 87 of this test case created by

Preparation code

<script>
var a=0;
function exec(i)
{
a+=i
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var arr = [];
  for(var i = 0; i < 2500; i++)
  {
  arr.push(Math.random());
  }
  
  var a=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
weird loop (1)
for(var i = 0, l = arr.length >>> 0; i < l; i++)
{
  exec(arr[i]);
}
pending…
weird loop (2)
for(var i = 0 >>> 0, l = arr.length >>> 0; i < l; i++)
{
  exec(arr[i]);
}
pending…
weird loop (3)
for(var i = 0 >>> 0, l = arr.length >>> 0; i < l; i+=1)
{
  exec(arr[i]);
}
pending…
weird loop (4)
for(var i = 0 >>> 0, l = arr.length >>> 0; i < l; i=i+1)
{
  exec(arr[i]);
}
pending…
weird loop (5)
for(var i = 0 >>> 0, l = arr.length >>> 0; i < l; i=(i+1)>>>0)
{
  exec(arr[i]);
}
pending…
forEach
arr.forEach(exec);
pending…
loop
for(var i = 0, l = arr.length; i < l; i++)
{
  exec(arr[i]);
}
pending…
while
var i=arr.length;
while(i--)
{
  exec(arr[i]);
}
pending…
for 2
for (var i = -1,len = arr.length; ++i < len;)
{
  exec(arr[i]);
}
pending…
for 3
for (var i = arr.length-1; i > -1; i--)
{
  exec(arr[i]);
}
pending…
for 4
for (i = arr.length - 1; i >= 0; i--)
{
  exec(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