Loops

JavaScript performance comparison

Revision 44 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
    arr = arr.concat(arr, arr, arr, arr, arr, arr, arr, arr, arr);
};
</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 loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i];
  i++;
};
pending…
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i];
  i++;
};
pending…
Reverse while loop
var i = arr.length;
while (i--) {
  arr[i];
};
pending…
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
  arr[i];
};
pending…
Reverse do … while loop
var i = arr.length;
do {
  arr[i];
} while (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
for (var i = -1; ++i < arr.length;) {
  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
function foo(x) {
  x;
};
arr.forEach(foo);
pending…
For in
for (var i in arr) {
    arr[i];
};
pending…
For in Protected
for (var i in arr) {
  if (arr.hasOwnProperty(i)) {
    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