Map performance (v4)

Revision 4 of this benchmark created on


Setup

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

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

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

function forOfValues(map, target) {
	for (const val of map.values()) {
		if (val === target) {
			return val
		}
	}
}

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

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

function forEach(map, target) {
	map.forEach((_, val) => {
		if (val === target) {
			return val
		}
	})
}

function forValues(map, target) {
	for (let it = map.values(), val = null; val = it.next().value;) {
		if (val === target) {
			return val
		}
	}
}

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
forEach
forEach(map, target)
ready
forOfKeys
forOfKeys(map, target)
ready
values iterator
forValues(map, target)
ready
forOfValues
forOfValues(map, target)
ready

Revisions

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