Pre or Post Increment for loop

JavaScript performance comparison

Revision 138 of this test case created

Info

http://stackoverflow.com/questions/5349425/whats-the-best-to-loop-an-array-in-javascript

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
normal
var i = 0,
                                        max,
                                        myarray = [1,2,3,4,5,6,7,8,9,10];

                        for (i = 0, max = myarray.length; i < max; i += 1) {
                                // do something with myarray[i]
                        }
pending…
opt for
var i, myarray = [1,2,3,4,5,6,7,8,9,10];
                        for (i = myarray.length; i--;) {
                                // do something with myarray[i]
                        }
 
pending…
opt while
var myarray = [1,2,3,4,5,6,7,8,9,10],
                                        i = myarray.length;
                        while (i--) {
                                // do something with myarray[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