Iterating over arrays

JavaScript performance comparison

Revision 10 of this test case created

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…
for in
for (var i in arr) {
  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