iterating arrays

Benchmark created on


Setup

const data = new Array(1000).fill(0).map(() => Math.random())

Test runner

Ready to run.

Testing in
TestOps/sec
legacy for loop
let sum = 0;
for (let i = 0; i < data.length; i++) {
	sum += data[i];
}
ready
legacy for loop, caching length
let sum = 0;
for (let i = 0, n = data.length; i < n; i++) {
	sum += data[i];
}
ready
for-of loop
let sum = 0;
for (const v of data) {
	sum += v;
}
ready
forEach, arrow function
let sum = 0;
data.forEach(v => {
	sum += v;
});
ready
forEach, legacy function
let sum = 0;
data.forEach(function( v ) {
	sum += v;
});
ready
reduce, arrow function
const sum = data.reduce((a, b) => a + b, 0);
ready
reduce, legacy function
const sum = data.reduce(function(a, b) {
	return a + b;
}, 0);
ready

Revisions

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