Array.includes vs Set.has

Benchmark created on


Setup

const set = new Set();
const searchSet = new Set();
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
const pickRandom = () => chars[Math.floor(Math.random() * chars.length)];

for (let i = 0; i < 50; i++) {
	set.add(pickRandom() + pickRandom());
	searchSet.add(pickRandom() + pickRandom()
);
}

const arr = [...set];
const search = [...searchSet];

console.log({
	search,
	arr,
	set
})

Test runner

Ready to run.

Testing in
TestOps/sec
Array.includes
search.filter(it => arr.includes(it))
ready
Set.has
search.filter(it => set.has(it))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.