Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the IndexOf method in javascript more efficient than iterating through an array?

I have an array of JSON objects and I want to find the object with a certain property. I know this may look like a duplicate question, but please continue because I think it's slightly different than the previous questions.

A guy I work with suggested using IndexOf, and that got me thinking. Is there something similar to the $elemMatch feature in mongo? Is there some command that basically says, in pseudo-code, "get me the object with this property from this array"? With the iteration, I feel like the psuedo-code says "look at the first object in this array. If this object has this property, get me this object. If not, look at the second object in this array....."

I understand how to use IndexOf like my friend suggested, but the more I thought about it, I started thinking that the IndexOf method might be fewer lines of code, but doesn't it ultimately have to iterate through the objects in the array to find the index of the one I need? So if I want to do something to the object with this property, and I use the method IndexOf to get the index, I would then refer to the object like myArray[indexFromIndexOfMethod], and then modify it accordingly, correct? So, if javascript is iterating over the array itself to perform the IndexOf method, why don't I just write my own iteration and save a step? Now if the IndexOf method uses a more efficient way of locating the array element than just iterating through and checking each one, then it would make sense for me to use it. Otherwise, it does not make sense to use the IndexOf method if you can achieve the same results with a simple iteration.

like image 500
bigchrisf Avatar asked Dec 17 '15 13:12

bigchrisf


People also ask

Which is faster includes or indexOf?

indexOf will accept a regular expression but always return -1, which isn't too helpful. So while includes will be a tiny, tiny amount slower because it has to check if you passed it a regex, in reality this will make no difference to how fast your code runs.

Why do we use indexOf in Javascript?

Definition and Usage. The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Does indexOf loop Javascript?

indexOf does a bunch of type-checking and validation that the for loop and while loop ignore.

What is the difference between indexOf and search in Javascript?

indexOf is for plain substrings, search is for regular expressions.


1 Answers

Array.prototype.indexOf also just iterates over the array and returns the first index with a matching value. It's the same thing you can do with a loop. It may or may not be slightly faster than a for loop, since indexOf can be implemented in native code and optimised differently than your for loop can, but there's no fundamental difference.

If you often need to access specific values as fast as possible, it's worth indexing them by that value in an object. Meaning, if you want to find a specific object by its property .foo often, do this:

var byFoo = {}
for (var i = 0; i < myArray.length; i++) {
    byFoo[myArray[i].foo] = myArray[i];
}

This then allows you instantaneous access by using byFoo['baz'].

Of course this has the additional overhead of maintaining these indices in multiple copies possibly, but it will speed up the array access by many magnitudes. You need to weigh the pros and cons.

like image 66
deceze Avatar answered Nov 02 '22 04:11

deceze