Set has vs Array includes

Benchmark created on


Setup

const arraySource = [
  "frank",
  "george",
  "anne",
  "peter",
  "teddy"
];

const setSource = new Set();
for (let i = 0; i < arraySource.length; ++i) {
	setSource.add(arraySource[i]);
}

let toCheck = [];
for (let i = 0; i < 50; ++i) {
	if (i % 10 === 0) {
		toCheck.push(arraySource[i / 10]);
	} else {
		toCheck.push("something " + i);
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array includes
let includedCount = 0;
for (let i = 0; i < toCheck.length; ++i) {
	if (arraySource.includes(toCheck[i])) {
		includedCount++;
	}
}
ready
Set has
let includedCount = 0;
for (let i = 0; i < toCheck.length; ++i) {
	if (setSource.has(toCheck[i])) {
		includedCount++;
	}
}
ready

Revisions

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