Set vs includes for small data sets

Benchmark created on


Description

I want to compare the performance of creating a set and looking up vs creating an array and using includes

Setup

const thingsToCheckFor = {
	one: "One",
	two: "Two",
	three: "Three",
	four: "Four",
	five: "Five"
};

const dataToSearchThrough = Array(10_000).map(() => {
	const index = Math.floor(Math.random() * 6);
	let thing = thingsToCheckFor[index];
	if (index > 4) {
		thing = "Unfindable";
	}
	return {
		thing,
	};
})

Test runner

Ready to run.

Testing in
TestOps/sec
Create and use set
const toSearch = new Set([
thingsToCheckFor.one,
thingsToCheckFor.two,
thingsToCheckFor.three,
thingsToCheckFor.four,
thingsToCheckFor.five,
]);

dataToSearchThrough.forEach((item) => {
	toSearch.get(item.thing);
});
ready
Create and use array
const toSearch = [
thingsToCheckFor.one,
thingsToCheckFor.two,
thingsToCheckFor.three,
thingsToCheckFor.four,
thingsToCheckFor.five,
];

dataToSearchThrough.forEach((item) => {
	toSearch.includes(item.thing);
});
ready

Revisions

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