Re:Why perl has separate arrays and hashes: It's as if they ...
by __david__ on Tuesday June 21, 2011 @01:59PM
(#2679719)
> Friends don't let friends iterate over arrays with for...in.
Which is too bad because it looks so much better.
Re:Why perl has separate arrays and hashes: It's as if they ...
by masklinn on Tuesday June 21, 2011 @02:12PM
(#2679775)
I just use Array.prototype.[forEach|map|filter] instead, or the equivalent Underscore.js function (which aliases to native when it can) if IE-compatibility is needed.
If you need performances, nothing will beat C-style access anyway. And FWIW, using for...in on an array is slower than using Array.prototype.forEach.
edit: FWIW, even hand-rolling an each() function (in the style of underscore's) will be faster than for...in.
it's not even doable in JavaScript: bare objects (~hashes) are iterated with for...in, using that with arrays gives inconsistent results: the array's keys will be iterated as if they were (string) properties, there are no guarantees they'll be iterated in numerical order, and any enumerable property added to the array itself or any of its ancestors will be iterated over as well.
Friends don't let friends iterate over arrays with for...in.