array loop

JavaScript performance comparison

Revision 153 of this test case created

Info

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

Preparation code

<script>
  var myArray = [];
  for(var i=0;i++<333;){
    myArray.push(Math.random());
  }
</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
for i++
for (var i = 0; i < myArray.length; i++) {

}
pending…
for ++i
for (var i = 0; i < myArray.length; ++i) {

}
pending…
for i++ < len
for (var i = 0; i++ < myArray.length;) {

}
pending…
for ++i <= len
for (var i = 0; ++i <= myArray.length;) {

}
pending…
while(i++ < len)
var i = 0;
while (i++ < myArray.length) {

}
pending…
while(++i <= len)
var i = 0;
while (++i <= myArray.length) {

}
pending…
while(i++ < len) cache
var i = 0;
var len = myArray.length
while (i++ < len) {

}
pending…
while(++i <= len) cache
var i = 0;
var len = myArray.length
while (++i <= len) {

}
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