forEach vs for..of

Benchmark created on


Setup

const items = [];


for (let i = 0; i < 10_000; i++) {
	items.push(i);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array.prototype.forEach
items.forEach(item => item + item);
ready
for..of loop
for (const item of items) {
	item + item;
}
ready
Array.prototype.forEach - with side effects
const results1 = [];

items.forEach(item => results1.push(item));
ready
for..of loop - w/ side effects
const results2 = [];

for (const item of items) {
	results2.push(item);
}
ready

Revisions

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