Iterating over arrays

JavaScript performance comparison

Revision 5 of this test case created by johnjbarton

Info

few items

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = Array(100);
    (function () {
        for(var i=0; i<arr.length; i++) {
            arr[i] = i;
        }
    }());
   
    function assignValue(x) {
        values.push(x);
    }
};
</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
var values = [];
arr.forEach(assignValue);
pending…
for, counting up
var values = [];
for(var i=0; i<arr.length; i++) {
    values.push(arr[i]);
}
pending…
for, counting up, caching length
var values = [];
for(var i=0, len=arr.length; i<len; i++) {
    values.push(arr[i]);
}
pending…
for, counting down
var values = [];
for(var i=arr.length; i--; values.push(arr[i]));
pending…
for/in
var values = [];
for(var i in arr) values.push(arr[i]);
pending…
while counting down
var values = [], i = arr.length;
while( i-- ){
  values.push(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