Array Iteration

Benchmark published by System on

Description

Compare different ways to iterate over an array.

Setup

const arr = Array.from({length: 10000}, (_, i) => i);

Test Runner

Initializing...

Testing in
Test CaseOps/sec
for loop
let sum = 0;
for (let i = 0; i < arr.length; i++) {
  sum += arr[i];
}
ready
forEach
let sum = 0;
arr.forEach(val => sum += val);
ready
for...of
let sum = 0;
for (const val of arr) {
  sum += val;
}
ready
reduce
arr.reduce((sum, val) => sum + val, 0);
ready

Revisions

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

Revision 1
publishedby Systemon