Set

Benchmark created on


Setup

const list = new Set(Array.from({ length: 10000 }, (_, val) => val))
const target = 9112

function forOf(list, target) {
	let result;
	
	for (const val of list) {
		if (val === target) {
			result = val
		}
	}
	
	return result
}

function forEach(list, target) {
	let result;
	
	list.forEach((val) => {
		if (val === target) {
			result = val
		}
	})
	
	return result
}

function forOfValues(list, target) {
	let result;
	
	for (const val of list.values()) {
		if (val === target) {
			result = val
		}
	}
	
	return result
}

function forValues(list, target) {
	const values = list.values()
	let result;
	
	for (let i = values.length; i >= 0; --i) {
		if (values[i] === target) {
			result = values[i]
		}
	}
	
	return result
}

Test runner

Ready to run.

Testing in
TestOps/sec
for-of
forOf(list, target)
ready
for-of array
forOf(Array.from(list), target)
ready
for-of values
forOfValues(list, target)
ready
forEach
forEach(list, target)
ready
forValues
forValues(list, target)
ready

Revisions

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