Loops

JavaScript performance comparison

Revision 51 of this test case created by Tri

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var arr = new Array(100);
function foo(x) {
  x;
};



if(!Array.prototype.forEach){
    Array.prototype.forEach= function(action, that /*opt*/) {
        for (var i= 0, n= this.length; i<n; i++)
           if(i in this)
           action.call(that, this[i], i, this);
    };
}


</script>

Preparation code output

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 loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i];
  i++;
};
pending…
Reverse while loop
var i = arr.length;
while (i--) {
  arr[i];
};
pending…
Reverse for loop
for (var i = arr.length; i--;) {
  arr[i];
};
pending…
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i];
};
pending…
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i];
};
pending…
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i];
};
pending…
Native Array#forEach implementation
arr.forEach(function(x) {
  x;
});
pending…
Native Array#forEach implementation with named function
arr.forEach(foo);
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