forEach vs loop

JavaScript performance comparison

Revision 13 of this test case created by Ted One

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  for(var i = 0; i < 1000; i++) arr[i]=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
loop
for(var i = 0, l = arr.length; i < l; i++){ }
pending…
forEach
arr.forEach(function(){});
pending…
while
var j=arr.length;

while(--j>0){}
pending…
for 2
var i = -1;
var len = arr.length
for (; ++i < len;) {
   
}
pending…
for 3
for (var i = arr.length-1; i > -1; i--) {}
pending…
for 4
for (i = arr.length - 1; i >= 0; i--) {
    
}
pending…
while 2
var i = arr.length;
while(i--) {

}
pending…
for loop, most common form
for (var i = 0; i < arr.length; 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.

1 Comment

Ted One (revision owner) commented :

Note that the first while loop misses the first element of the array.