Add existing Set value with and without has()

Benchmark created on


Setup

const arr = Array.from({ length: 1000 }, (_, i) => i);

const arrOdd = arr.filter(n => n % 2 != 0);
const arrEven = arr.filter(n => n % 2 === 0);

const setOdd = new Set(arrOdd)
const setEven = new Set(arrEven)

Test runner

Ready to run.

Testing in
TestOps/sec
check if value is exists using has()
arr.forEach((n) => {
	if (!setOdd .has(n)) {
		setOdd.add(n)
	}
})
ready
not checking if value is exists using has()
arr.forEach((n) => {
	setEven.add(n)
})
ready

Revisions

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