For loop, implicit index test

JavaScript performance comparison

Test case created by MeBerserk

Info

Is there a difference between: for (var i = length; i > 0; i--) and for (var i = length; i; i--)

Preparation code

Preparation code
<script>
  // Populate the base array
  var arr = [];
  for (var i = 0; i < 1000; i++) {
    arr[i] = 'value' + i;
  }

  function someFn(ix) {
    return ix * 5 + 1 / 3 * 8;
  }
</script>

Preparation code output

Preparation code

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 i < length; i++
for (var i = 0, len = arr.length; i < len; i++) {
  someFn(i);
}
pending…
for i > 0; i--
for (var i = arr.length; i > 0; i--) {
  someFn(i);
}
pending…
for i > 0; i
for (var i = arr.length; i; i--) {
  someFn(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