How can I effectively perform collection membership checks in Javascript? I have a potentially large array of strings and I need to check if a given string is a member of the array.
Initially, I thought that the operator incould help, but after reading the documents in the Mozilla Developer Network, I found that its purpose is different . In Javascript, it checks if the specified property is specified in the specified object.
For performance reasons, I would rather use js builtin, but if such a function does not exist, I will probably end up doing one of the following:
- use an array to create an object with array elements as keys, and then use
in - iterate over the elements of an array and a comparison element over an element
- implement binary search
Any opinion? Or the best ideas?
thank
Paolo source
share