Array.find vs Map vs Object (v2)

Revision 2 of this benchmark created on


Setup

var valuesArr = Array.from({ length: 1_000 }).map((_, idx) => [idx, `str${idx}`]);
var valuesMap = new Map(valuesArr);
var access = Array.from({ length: valuesArr.length / 2 }).flatMap((_, idx) => [idx * 2]);
var valuesObj = Object.fromEntries(valuesArr);

console.log(valuesArr, valuesMap, access);

Test runner

Ready to run.

Testing in
TestOps/sec
Map access
for (const idx of access) {
	if (valuesMap.get(idx) === undefined) {
		throw new Error('x');
	}
}
ready
Array.find
for (const idx of access) {
	if (valuesArr.find(x => x[0] === idx) === undefined) {
		throw new Error('x');
	}
}
ready
Object access
for (const idx of access) {
	if (valuesObj[idx] === undefined) {
		throw new Error('x');
	}
}
ready

Revisions

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