Loops

JavaScript performance comparison

Revision 121 of this test case created by Visha

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    for (var i = 0; i < 1000; i++) {
      arr.push(i);
    }
    var arrLen = arr.length;
};
</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 (uncached)
var i = 0;
while (i < arr.length) {
  arr[i] += 1;
  i++;
};
pending…
While Loop (cached)
var i = 0;
while (i < arrLen) {
  arr[i] += 1;
  i++;
};
pending…
While Loop (reversed)
var i = arr.length;
while (i--) {
  arr[i] += 1;
};
pending…
While Loop (reversed, postprocessed)
var i = arr.length;
while (i-- > 0) {
  arr[i] += 1;
};
pending…
Do-While Loop (reversed)
var i = arr.length;
do {
  arr[i] += 1;
} while (i--);
pending…
For Loop (reversed)
for (var i = arr.length; i--;) {
  arr[i] += 1;
};
pending…
For Loop (uncached, proprocessed)
for (var i = 0; i < arr.length; ++i) {
  arr[i] += 1;
};
pending…
For Loop (cached, proprocessed)
for (var i = 0; i < arrLen; ++i) {
  arr[i] += 1;
};
pending…
For Loop (uncached, proprocessed)
for (var i = -1; ++i < arr.length;) {
  arr[i] += 1;
};
pending…
For Loop (cached, proprocessed)
for (var i = -1; ++i < arrLen;) {
  arr[i] += 1;
};
pending…
Foreach (native function)
arr.forEach(function(x) {
  x += 1;
});
pending…
Foreach (function call)
function foo(x) {
  x += 1;
};
arr.forEach(foo);
pending…
For Loop (uncached) Simple
var i;
for (i = 0; i < arr.length; i += 1) {
  arr[i] += 1;
}
pending…
For Loop (cached) Simple
var i;
for (i = 0; i < arrLen; i += 1) {
  arr[i] += 1;
}
pending…
Special For Loop (reversed, cached)
for (var i = arrLen; i--;) {
  arr[i] += 1;
};
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