forEach vs loop

JavaScript performance comparison

Revision 9 of this test case created

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 unknown unknown
Test Ops/sec
forEach
arr.forEach(function(){});
pending…
loop
for(var i = 0, l = arr.length; i < l; i++){ }
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…

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. Here’s a list of current revisions for this page:

1 comment

Ted One commented :

Your while loop misses the first element of the array. Use the postfix decrement operator instead, like this:

while(j-->0){}

Add a comment