r/learnjavascript • u/AccomplishedAge1907 • Sep 18 '26
I benchmarked five ways to check array membership against the actual data in my project
Last month I was optimizing a filtering component that runs lookups against a list of selected IDs. The list sits at about 40 entries during normal use and climbs to around 3,200 when a bulk import lands. I had been using includes() everywhere without thinking about it, so I wrote a small harness to compare five approaches against my real data at both sizes.
At 40 items, everything was indistinguishable. Under a microsecond per call for all five. The ranking only appeared at 3,200. Set.has() was fastest by a wide margin, roughly 50x faster than includes() and indexOf(), which were nearly identical. A plain for loop with an early break landed between those two groups. Array.find() was slowest, which I expected because of the callback overhead.
The surprise was how early Set construction pays for itself. I assumed building the Set only made sense if I was searching thousands of times. On my data the crossover was around seven lookups. After seven calls the Set was already cheaper than includes() from scratch each time. I used Plan Mode in verdent to outline the refactor before changing anything. Then I swapped every hot path to a Set built once per filter cycle.
At 3,200 items and 60 lookups per render, total lookup time dropped from 1.8ms to 0.04ms.