Map look-ups with return result (v2)

Revision 2 of this benchmark created on


Setup

const map = new Map(Array.from({ length: 10000 }, (_, val) => [val, true]))
const target = 9112

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

function forArr(map, target) {
	const arr = Array.from(map)
	let result;
	
	for (let i = arr.length - 1; i >= 0; --i) {
		if (arr[i][0] === target) {
			result = arr[i][0]
		}
	}
	
	return result
}

function forOfArr(map, target) {
	let result;
	
	for (const [val] of Array.from(map)) {
		if (val === target) {
			result = val
		}
	}
	
	return result
}

Test runner

Ready to run.

Testing in
TestOps/sec
for-of
forOf(map, target)
ready
for-reversed
forArr(map, target)
ready
array for-of
forOfArr(map, target)
ready

Revisions

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