So lets say I\'ve added some prototype methods to the Array class:
Array.prototype.containsKey = function(obj) {
for(var key in this)
if (key =
For high-performance iteration over JavaScript arrays, use either a for
or while
loop. Nicholas Zakas discusses the most-performant options for iterating over arrays in his Tech Talk Speed Up Your JavaScript.
Your best bet is probably something like this:
for (var i = collection.length - 1; i >= 0; i--) {
if (obj == collection[i]) return true;
}
This approach will be peform best for a few reasons:
length
property is only accessed once, at the initialization of the loopi >= 0
) instead of to another variable