how could I efficiently do collection membership checks in Javascript? I have a potentially large array of strings and I need to verify if a given string is a member of the array.
Initially I thought that the in
operator could help, but after reading the docs on Mozilla Developer Network I discovered that its purpose is different. In Javascript it checks if the specified property is in the specified object.
For performance related reasons I'd prefer to use a js builtin, but if a such function doesn't exist I'll probably end to do one of the following:
- use the array to create an object having array elements as keys and then use
in
- iterate over array elements and do the comparison item by item
- implement a binary search
Any opinion? Or better ideas?
Thanks