while vs forr

JavaScript performance comparison

Revision 2 of this test case created by Tim Vermaelen

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = [1, 2, 3, 4, 5, 6, 7, 8];
};
</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
var i = -1,
    n = a.length;
while (++i < n) {
  a[i];
};
pending…
for i++
for (var i = 0, n = a.length; i < n; i++) {
  a[i];
};
pending…
for ++i
for (var i = -1, n = a.length; i < n; ++i) {
  a[i];
};
pending…
while--
var len = a.length;
while (len--) {
  a[len];
}
pending…
for i++ (2)
var i = 0,
    n = a.length;
for (i; i < n; i++) {
  a[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