views:

76

answers:

4

Would it be wrong to conclude that the fastest way to loop over a JavaScript array would be to use the for-in loop? E.g.:

for (var index in items) {
    ...
}

See http://jsperf.com/loop-test2/2

+5  A: 

The for-in statement shouldn't be used to iterate over array or array-like elements.

The purpose of this statement to enumerate object properties.

It shouldn't be used for array-like objects because:

  • The order of iteration is not guaranteed, the array indexes may not visited in the numeric order.
  • Inherited properties are also enumerated.

To iterate over an array, a sequential loop is always recommended.

Recommended article:

Edit: Oh also, I forgot to mention that your test is completely biased, because the expression new Array(10000) simply initializes an Array object with 10000 as the value of the length property, the numeric index properties don't even exist, that's why it seems to be the fastest e.g.:

var a = new Array(10);
a.length; // 10
a.hasOwnProperty('0'); // false, the indexes don't even exist!
a.hasOwnProperty('1'); // false
//...

Try this fair test, with an array object that really contains 10000 elements and you will be surprised. :)

CMS
The order of iteration doesn't matter because 'index' will always be the numeric index of the enumerated element.
SnickersAreMyFave
@SnickersAreMyFave - No, it's not. Test this in IE: http://jsfiddle.net/nick_craver/z52gx/
Nick Craver
@CMS +1 Do you count the number of times you've answered this question? :)
galambalazs
@SnickersAreMyFave: Forgot to mention something about your test, edited.
CMS
@galambalazs: Thanks :), Yeah, many times, and I will keep doing it, this is a really common anti-pattern :(
CMS
+2  A: 

Yes it can be the fastest, but it isn't safe and not a for-in loop's purpose...so it's really a moot point.

Something being fast isn't a concern until it's correct :)

For example test this in IE: http://jsfiddle.net/nick_craver/z52gx/ You'll notice the order is incorrect.

Nick Craver
Why do you say it's fastest? For a 10,000 item array, it could be faster, but for more real-life scenarios, I'm not so sure: http://jsperf.com/array-indexing-performance
Philippe Leybaert
@Philippe - sorry I meant with respect to his specific test, I updated to clarify this, my intent was more to convey you shouldn't use it in the first place, so whatever the performance, it doesn't matter if it's not correct in the first place.
Nick Craver
+1  A: 

Whether or not it's the fastest is implementation dependent. Consider that each major browser vendor has their own implementation, plus a mobile implementation, plus all the old browsers still in use and beta browsers demoing next-gen javascript engines... there's "only" about 30 different javascript implementations out there right now. And even if this is currently fastest on all or most of those, javascript engines are still rapidly evolving.

And that's even assuming that javascript is your bottleneck. Here's a tip - as slow as javascript is, it's rarely the bottleneck for page rendering. Internet tranfer speeds and server response times tend to be much more important factors.

Maybe it's a better idea to code for correctness first.

Joel Coehoorn
A: 

This is a little out of date (Only includes FF3.0 and IE8b), but it's pretty comprehensive.

http://blogs.sun.com/greimer/entry/best_way_to_code_a

Reverse while loops with a simplified condition look the fastest:
var len; while(len--){}

Uldeim