2022-10-30T12:05:00

Array.prototype.includes is bad sometimes

When using JavaScript I mostly use array.includes() to check arrays for specific values, usually iterated over an array. Up until now, this has not been an issue, performance wise.

When looping over hundreds of thousands of values however, Array.prototype.includes runs into performance issues as it has a complexity of O(n). Also as I usually run this inside a for loop, the whole operation becomes O(n²).

Set.prototype.has however runs in O(n) (or approximately that, I think) which made a huge difference in my case. I was able to improve performance of the function by x811. That is from 39.735 seconds using Array.prototype.includes to 48.624 milliseconds using Set.prototype.has.

 
Go back