Fastest Array Loops

JavaScript performance comparison

Revision 48 of this test case created by Terry Anastasiadis

Info

Testing the best overall loop performers.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // Populate the base array
    var arr = [];
    for (var i = 0; i < 1000; i += 1) {
      arr.push( 0.9999999999999999 );
    }
    //Something to do...
    function doStuff(x) {
      return (x * x * 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
While, length -= 1
var l = arr.length;
while (l -= 1) {
  doStuff(arr[l]);
}
pending…
While, i += 1
var i = 0,
    l = arr.length;
while (i < l) {
  doStuff(arr[i]);
  i += 1;
}
pending…
For, i += 1
for (var i = 0, l = arr.length; i < l; i += 1) {
  doStuff(arr[i]);
}
pending…
For, outter, i += 1
var i = 0,
    l = arr.length;
for (; i < l; i += 1) {
  doStuff(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