Set (v3)

Revision 3 of this benchmark created on


Setup

const list = new Set(Array.from({ length: 10000 }, (_, val) => val + 1))
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) {
	let result;
	
	for (let it = list.values(), val = null; val = it.next().value;) {
		if (val === target) {
			result = val
		}
	}
	
	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.