Quantcast
Channel: How do I check if an array includes a value in JavaScript? - Stack Overflow
Viewing all articles
Browse latest Browse all 66

Answer by Rohìt Jíndal for How do I check if an array includes a value in JavaScript?

$
0
0

If you're just trying to check whether a value is included in a collection, It would be more appropriate to use a Set, As Arrays can have duplicate values whereas Sets cannot. Also, Replacing array.includes with set.has improves the performance from O(n2) to O(n). This will be useful when you have to look up multiple values for the same Set. so if you're just going to look up a single value, there's no benefit to use set.has, you can just use an array.includes.

Created a jsbench demo, You can run this to check the performance.

Screenshot of the test execution :

enter image description here


Viewing all articles
Browse latest Browse all 66

Trending Articles