3

What is the fastest method to check if a number is in a list in Javascript?

I know about indexOf >= but it seems rather slow to me.

I have to perform millions of checks per second and the list is rather short (max ~10 entries)

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
pixartist
  • 1,137
  • 2
  • 18
  • 40
  • 3
    An O(n) search through 10 items may actually be _faster_ than a constant-time lookup, due to the difference in baseline overhead. – Matt Ball Aug 10 '13 at 12:44
  • 4
    Millions of checks per second? Really? – Lee Taylor Aug 10 '13 at 12:46
  • 2
    Are all checks against the same list? – georg Aug 10 '13 at 12:50
  • 1
    How large is the range of values? – Pointy Aug 10 '13 at 12:53
  • 1
    If the data does not change frequently, you can sort, then use binary search on them. That is if actually you have millions of records. Because of @MattBall's reason. Otherwise use an object instead (a dictionary or hash table in other languages) – Yaw Boakye Aug 10 '13 at 12:57
  • In 2021, one should use [`new Set([ 2, 3, 5, 7, 11 ]).has(7)`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set). – Sebastian Simon Aug 24 '21 at 21:29

3 Answers3

2

Try it out at jsperf but I suspect that using an object and setting up the numbers as properties would be faster than an array search.

var theList = { 1: true, 2000: true, 253: true, -12077: true, ... };

if (theList[ someNumber ]) { // see if some number is in the list

Now, that said, you're not going to be able to do anything useful in JavaScript in a web browser millions of times per second, except perhaps on extremely high-end machines that aren't doing much else.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

Well it is better to use indexof()

As an alternative which is not appreciated in your case is to use Enumerable#include

You can use Enumerable#include but I doubt if it is faster than indexof() Something like:-

[1, 2, '3', '4', '5'].include(3);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • You're suggesting he load the prototypejs library for this? And that it'll be the fastest solution? –  Aug 10 '13 at 12:49
  • I have just given him an another alternative. I have mentioned that it is not faster but OP doesnot want to use indexof(). – Rahul Tripathi Aug 10 '13 at 12:50
  • Well that's the reason for the -1. OP isn't looking for random alternatives, and it would be silly to include a large library for a single method that does basically the same thing as he's currently doing. –  Aug 10 '13 at 12:51
  • Yes I fully agree that including a libarary is a bad idea. But then in that case OP doesnot have a choice other than IndexOf() ;) – Rahul Tripathi Aug 10 '13 at 12:53
  • @RahulTripathi that's not really true, however; there is at least one other choice. – Pointy Aug 10 '13 at 12:56
  • A `for` loop or an object property lookup are likely much faster. –  Aug 10 '13 at 12:57
  • @Pointy:- Seen your answer Sir. Thats a good one. But are we really sure that this is the structure of list which OP has? – Rahul Tripathi Aug 10 '13 at 12:57
  • 1
    @RahulTripathi no, but conversion of a short (remember, not more than ten items) list in an array to a list as an object is trivial. – Pointy Aug 10 '13 at 12:59
  • @CrazyTrain:- Fully agree. But I still think that using IndexOf() would be a better idea. Kindly correct me if I am wrong. P.S:- Updated my answer as well!!! :) – Rahul Tripathi Aug 10 '13 at 13:00
  • @Pointy:- Agreed. +1 :) – Rahul Tripathi Aug 10 '13 at 13:01
  • 1
    If OP really needs this lookup millions of times per second *(which sounds a little suspicious IMO)*, then he's going to need every bit of optimization that can be found. In such a case, one often needs to trade short code for optimized code. I don't think any of us really knows enough about the actual situation to offer the best solution. –  Aug 10 '13 at 13:04
  • @CrazyTrain:- Millions of times per second is really questionable!! ;) P.S. Do you really think that still I deserve a downvote? :( – Rahul Tripathi Aug 10 '13 at 13:09
  • Yes, you've not provided a reasonable solution that would improve performance over `.indexOf()`. –  Aug 10 '13 at 13:16
0

If you're looking for speed of comprehension, use array.includes:

[-1, 1, 2].includes(0)  // false
[-1, 1, 2].includes(-1) // true
[-1, 1, 2].includes(-2) // false
[-1, 1, 2].includes(2)  // true
[-1, 1, 2].includes(3)  // false
vhs
  • 9,316
  • 3
  • 66
  • 70