forEach vs for

Benchmark created on


Setup

const cells = [];
for (let i = 0; i < 100; i++) {
	cells[i] = []
	for (let j = 0; j < 100; j++) {	
		cells[i][j] = i * j
	}
}
console.log(cells)

Test runner

Ready to run.

Testing in
TestOps/sec
ForEach
let sum = 0;
cells.forEach((row, rowIndex) => {
	row.forEach((cell, colIndex) => {
		sum += cell;
	})	
})
ready
For
let sum = 0;
for (let i=0, n=cells.length; i < n; i++) {
	const row = cells[i];
	for (let j=0, m=row.length; j < m; j++) {	
		sum += row[j];
	}
}
ready

Revisions

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