10

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?

4
  • 1
    Can I ask how you are identifiying that this is a performance issue? (just out of curiosity) Commented Mar 24, 2011 at 14:34
  • 1
    To be honest, I don't remember. Commented Mar 27, 2011 at 15:53
  • Fair enough I didn't notice that the question was over a year old... O.K do you even remember what you meant by according to 'my profiler'? What profiler are you using? Commented Mar 27, 2011 at 16:33
  • I was probably using Firebug... Commented Mar 27, 2011 at 17:46

5 Answers 5

15

Well internally inArray makes a simple loop, I would recommend you to check if there is a native Array.prototype.indexOf implementation and use it instead of inArray if available:

function findPoint(point, list) {
  var l = list.map(function anonMapToId(p) { return p.id });
  var found = ('indexOf' in Array.prototype) ? l.indexOf(point.id)
                                             : jQuery.inArray(point.id, l);
  return found;
}

The Array.prototype.indexOf method 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.

Sign up to request clarification or add additional context in comments.

3 Comments

jQuery already does this automatically. forum.jquery.com/topic/inarray (It's on the first comment to the post) As an aside, IE8 doesn't implement Array.prototype.indexOf.
'inArray' sounds more like it returns a boolean, but it does not. Do the next developer a favor and use 'indexOf'.
I think it will defeat the purpose of "speed" if you don't cache the result of the test for the existence of the indexOf method. I also believe that jQuery will do that for you...
3

What you really want is a Array.prototype.filter.

function findPoint(point, list)
{
     return list.filter(function anonFilterToId(p) { 
         return p.id === point.id; 
     }).length > 0;
}

Comments

2

Even is the inArray function were slow, you're still creating a full new array for every search. I suppose it would be better to redesign this search, by e.g. creating the id-list before finding the points, and using that one to search into:

Comments

2

I'm doing a join of the array to turn it into a string and avoid the loop section like this :

var strList = ","+array.join(",")+",";
return strList.indexOf(","+search+",") !== -1 ? true : false;

if the array is huge, it can hurt, but for a small list it's much faster than the loop solution

PS I'm adding an ending coma to avoid look a like

1 Comment

This may not be the prettiest way but it eliminated the "Stop running this script" error I was getting with inArray in IE8.
1

I always use lastIndexOf when I want to know if there's a string in my array. So, its something like this:

var str = 'a';
var arr = ['a','b','c'];

if( arr.lastIndexOf(str) > -1){ 

      alert("String " + str + " was found in array set");

} else {

      alert("String " + str + " was not found");

}

If you just want to find a string in array, I do believe this might be the best practice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.