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(el => el + el);
ready
for..of loop
for (const el of items) {
	el + el;
}
ready
Array.prototype.forEach - w/ side effects
const results1 = [];

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

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

Revisions

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