Performance cost of running a loop

Benchmark created by Daniel Gallagher on


Description

A speed comparison between a loop, and a loop nested inside a conditional block that may not be run (isn't, in this case) - intended to check the overhead of running a loop unnecessarily. I feel I've made a mistake in the test as the results are very surprising.

Test runner

Ready to run.

Testing in
TestOps/sec
Conditional
if (!true) {
  for (var l = 0; l < 1; l++) {
    continue;
  }
}
ready
Loop once
for (var m = 0; m < 1; m++) {
    continue;
}
ready
Loop 100 times
for (var n = 0; n < 100; n++) {
    continue;
}
ready
Loop 10000 times
for (var o = 0; o < 10000; o++) {
      continue;
}
ready
Loop 35000 times
for (var p = 0; p < 35000; p++) {
    continue;
}
ready

Revisions

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

  • Revision 1: published by Daniel Gallagher on