Loops - increment vs decrement

JavaScript performance comparison

Revision 6 of this test case created

Info

Tests the speed gains when decrementing through a loop vs incrementing through a loop.

Preparation code

<script>
  var a = [1, 43, 65, 7, 3, 2, 6, 8, 9, 5, 19];
  var temp;
</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
Decrement Loop
for (var i = a.length; i > 0; i--) {
 temp = a[(a.length - i)];
}
pending…
Increment Loop
for (var i = 0; i < a.length; i++) {
 temp = a[i];
}
pending…
Decrement - one condition
for (var i = a.length; --i;) {
 temp = a[(a.length - i)];
}
pending…
Decrement - one condition, one subtraction
for (var i = a.length - 1; i--;) {
 temp = a[i];
}
pending…
while loop
var i = a.length - 1;
while(i--)
{
 temp = 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