for vs while

JavaScript performance comparison

Revision 11 of this test case created by Milan Adamovsky and last updated

Info

for vs while

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var result = 0,
        steps = 100;
};
</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
for
var result = 0;
for (var i = steps - 1; i >= 0; i--) {
  result += i;
}
pending…
optimised for
var result = 0;
for (var i = steps; i--;) {
  result += i;
}
pending…
while
var i = steps, result =0;
while (i--) {
  result += i;
}
pending…
for i++
var result = 0;
for (var i = 0; i < steps; i++) {
  result += i;
}
pending…
for inline
var result = 0;
for (var i = 0; i < steps; i++) result += i;
pending…
for infinite
var i = 0, result =0;
for (;;) {
  result += i++;
  if (i == steps) break;
}
pending…
for v2
var i, result = 0;
for (i = steps - 1; i; i--) {
  result += i;
}
pending…
while 2
var i = steps, result = 0;
while (i) {
  result = result + i;
  i = ~-i;
}
pending…
holy grail
//var x = "Read my blog about it at http://milan.adamovsky.com";

// Seems to work best for IE
var i = steps, result = 0;
redo:
while (i--) {
  result = result + i;
  continue redo;
}
pending…
holy grail 2
var i = steps, result = 0;

// Seems to work best for non-IE
redo:
while (i) {
  result = result + i;
  i--;
  continue redo;
}
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:

3 comments

Milan Adamovsky (revision owner) commented :

Aren't the results incredible??

Alireza commented :

Very Nice ...

thanks

Milan Adamovsky (revision owner) commented :

The tests got fixed. They yielded skewed results. The quest goes on.

Add a comment