Map look-ups with return result

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 = val
		}
	}
	
	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
forOf
forOf(map, target)
ready
forArr
forArr(map, target)
ready
forOfArr
forOfArr(map, target)
ready

Revisions

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