Iterating over arrays

JavaScript performance comparison

Revision 7 of this test case created by Jörg Schaper

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = new Array(1000);
    (function () {
        for(var i=0; i<arr.length; i++) {
            arr[i] = i;
        }
    }());
   
    var value;
};
</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 (x) {
    value = x;
});
pending…
for, counting up
for(var i=0; i<arr.length; i++) {
    value = arr[i];
}
 
pending…
for, counting up, caching length
for(var i=0, len=arr.length; i<len; i++) {
    value = arr[i];
}
 
pending…
for, counting down
for(var i=arr.length; i>=0; i--) {
    value = arr[i];
}
 
pending…
while, counting down (1)
var i = arr.length;
while (i--) {
  value = arr[i];
}
pending…
while, counting down (2)
var i = arr.length;
while (i>0) {
  value = 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. Here’s a list of current revisions for this page:

0 comments

Add a comment