I’m doing very frequent searches in arrays of objects and have been using jQuery.inArray(). However, I’m having speed and memory issues and one of the most called methods according to my profiler is jQuery.inArray(). What’s the word on the street about its performance? Should I switch to a simple for loop?
My specific function is:
function findPoint(point, list)
{
var l = list.map(function anonMapToId(p) { return p.id });
var found = jQuery.inArray(point.id, l);
return found;
}
Is perhaps list.map() is more to blame?
Well internally
inArraymakes a simple loop, I would recommend you to check if there is a nativeArray.prototype.indexOfimplementation and use it instead ofinArrayif available:The
Array.prototype.indexOfmethod has been introduced in browsers that implement JavaScript 1.6, and it will be part of the ECMAScript 5 standard.Native implementations are way faster than non native ones.