.forEach vs .find

Benchmark created on


Setup

let rows = [];
for (let i = 0; i < 1000; i++) {
	rows.push({id: i});
}

const target = Math.floor(Math.random() * 1000)

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
let found;
rows.forEach(row => {
	if (row.id === target) {
		found = row;
	}
});

console.log(found)
ready
find
let found = rows.find(row => row.id === target);
//if (found) {
	console.log(found);
//}
ready

Revisions

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