Object access vs. Array.find (v3)

Revision 3 of this benchmark created on


Setup

let arr = [];
let obj = {};
let ids = [];

for (let i=0; i<1000; i++) {
	let letter = String.fromCharCode(i%26 + 65);
	let letterCount = Math.floor(i/26) + 1;
	let id = letter.repeat(letterCount);
	let entity = {id, value: id};
	arr[i] = entity;
	obj[id] = entity;
	if (i % 100 === 0) {
		ids.push(id);
	}
}

Teardown

arr = [];
obj = {};
ids = [];

Test runner

Ready to run.

Testing in
TestOps/sec
Array.find
ids.forEach(id => {
	const entity = arr.find((entity) => entity.id === id);
});
ready
Object access
ids.forEach(id => {
	const entity = obj[id];
});
ready
for of (array)
ids.forEach(id => {
	let entity;
	for (element of arr) {
		if (element.id === id) {
			entity = element;
			break;
		}
	}
})
ready

Revisions

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