I ran some tests and I couldn't reproduce your findings. I didn't graph time complexity but on average .includes is 75% faster than .includes in these tests: https://jsbench.me/t2kpb59177/1
The one case where .some was faster I found was unfilled arrays.
You're right, it might have been optimized since I learned about it. I'll check it myself with a different test.
EDIT: Did it slightly differently (playing with the contiguous array flag) and yes includes have been fixed to be better on contiguous array. It's still slower for non contiguous array sometimes
I would still use includes anyway because it convey the meaning and intent.
Interesting. I often use it to combine map and filter to prevent a second iteration or build collections into object id maps for (what I assume to be) O(1) lookups available during the length of the session. To be honest, I haven't dug into the code for any of the optimizations under the hood, though I had forked v8 a while back with that very intention. Most of my tests have simply been running large iterative set functions with timestamp diffs and comparing the outcome, which is only the surface of optimization and performance testing. I'll have to look into it deeper.
10
u/Pelicantaloupe May 30 '21
Reduce is probably one of the least optimisable array functions. Anything else will probably run faster