DEV Community

Discussion on: A neat little trick with JavaScript's indexOf()

Collapse
 
bgadrian profile image
Adrian B.G.

It flips all bits of a number, I'm not sure how it exactly works ... So that means, we can take advantage of it and use it in indexOf (haven't seen any other uses yet though).

facepalm, why would you use something you don't know how it works?
As a professional you must guarantee that your code works in all conditions, and you cannot do that if you don't understand how it works.

  1. This snippet I would consider a bad code, most devs will have a hard time understanding it, and that extra time it takes to read&understand will be added for each developer * each time it reads that code. So basically losing resources (time = money).

  2. You do not use an explicit boolean expression, you let the language to cast (all non-zero elements to true), again I think our code must be explicit if (something != 0) would be better than if (something),for us humans.

  3. indexOf is not efficient, it will do a linear search, that means, in worst case, it will:

    • iterate trough all the elements
    • compare each element to your argument
    • probably doing a cast if the types are different

If you want to do that often you can use other data structures that are more efficient for lookups or if the array is sorted a more optimal search algorithm (ex: binary search).

Please keep the "neat" tricks for hackatons, learning projects and meetups, not for production code. We write code for humans, our peers, and bit operators are for machines, we think in decimal.

As for how it works, it has something to do with how the runtimes store the negative numbers

Collapse
 
werninator profile image
Patrick Werner • Edited

Thanks for your opinion and the URL, I'm not trying to convince anyone to use this piece of advice/tip or how you wanna call it - everyone has to decide on his/her own if they want to experiment with it or even use it. I'll use it because my projects mostly consist of legacy code from 2002 that is much worse than a little operator combined with a non-efficient, but established method :c)

Collapse
 
bgadrian profile image
Adrian B.G.

Do not get me wrong, I commented because I felt that junior devs (that are usually attracted by this kind of posts) should know the other side of the story. You presented the story only for one side so I felt compelled to add the side effects.

Also experimenting is good, I recommend to you and readers:

  • read how and why this trick works
  • test the operator on negative and positive numbers, zero and minimum-maximum numerical values
  • do some benchmarks with jsperf on different methods of searching a specific value in a specific list, including with same types indexOf(1), indexOf("1")
  • learn bit operators, you will not used them often, but when you need them they will bring that extra performance boost, most likely in a critical path of your app

As for legacy code, too bad they don't evolve over time, as our skills and knowledge growth, our code should too.

As other said, Array.includes was added to ECMAScript just because indexOf > -1 was very popular. but, they are not fully interchangeable because they do not use the same comparison algorithm: results of indexOf and includes may be different.

[1,NaN,3].includes(NaN)
true
[1,NaN,3].indexOf(NaN)
-1