For Loop vs. Optimized For Loop

JavaScript performance comparison

Test case created by

Preparation code

<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
<a href="#">Test</a>
    

Preparation code output

<a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a> <a href="#">Test</a>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Customary For Loop
var anchors = document.getElementsByTagName("a");

for (i = 0; i < anchors.length; i++) {
  anchors[i].style.color = "red";
}
pending…
Optimized For Loop
var anchors = document.getElementsByTagName("a");

for (var i = anchors.length - 1; i >= 0; i -= 1) {
  anchors[i].style.color = "red";
}
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.

1 Comment

Babi Sz commented :

Wow!!! thank you for this. I have used the optimised version for embedding youtube videos with JS and the download is so much faster!! Many, many thanks

Test Test Test Test Test Test Test Test Test Test