for vs while (v11)

Revision 11 of this benchmark created by Milan Adamovsky on


Description

for vs while

Setup

var result = 0,
        steps = 100;

Test runner

Ready to run.

Testing in
TestOps/sec
for
var result = 0;
for (var i = steps - 1; i >= 0; i--) {
  result += i;
}
ready
optimised for
var result = 0;
for (var i = steps; i--;) {
  result += i;
}
ready
while
var i = steps, result =0;
while (i--) {
  result += i;
}
ready
for i++
var result = 0;
for (var i = 0; i < steps; i++) {
  result += i;
}
ready
for inline
var result = 0;
for (var i = 0; i < steps; i++) result += i;
ready
for infinite
var i = 0, result =0;
for (;;) {
  result += i++;
  if (i == steps) break;
}
ready
for v2
var i, result = 0;
for (i = steps - 1; i; i--) {
  result += i;
}
ready
while 2
var i = steps, result = 0;
while (i) {
  result = result + i;
  i = ~-i;
}
ready
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;
}
ready
holy grail 2
var i = steps, result = 0;

// Seems to work best for non-IE
redo:
while (i) {
  result = result + i;
  i--;
  continue redo;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by antimatter15 on
  • Revision 2: published by David Calhoun on
  • Revision 3: published by Leonardo Dutra on
  • Revision 4: published by Anthony M on
  • Revision 5: published on
  • Revision 6: published on
  • Revision 7: published on
  • Revision 8: published by devu on
  • Revision 9: published by Jörn Berkefeld on
  • Revision 11: published by Milan Adamovsky on
  • Revision 18: published by Milan Adamovsky on
  • Revision 24: published by Milan Adamovsky2 on
  • Revision 30: published by Jasper on