Map look-ups with return result (v3)

Revision 3 of this benchmark created on


Setup

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

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

function forOfKeys(map, target) {
	let result;
	
	for (const val of map.keys()) {
		if (val === target) {
			result = val
		}
	}
	
	return result
}

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

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

function forOfEntries(map, target) {
	let result;
	
	for (const [val] of map.entries()) {
		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
}

function forEach(map, target) {
	let result;
	
	map.forEach((_, val) => {
		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
for-of-keys
forOfKeys(map, target)
ready
for-of-entries
forOfEntries(map, target)
ready
forEach
forEach(map, target)
ready
forKeys
forKeys(map, target)
ready
forValues
forValues(map, target)
ready

Revisions

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