Script Preparation code:
x
 
function generateTestArray() {
  const result = [];
  for (let i = 0; i < 1000000; ++i) {
    result.push({
      a: i,
      b: i / 2,
      r: 0,
    });
  }
  return result;
}
const array = generateTestArray();
Tests:
  • for

     
    for (let i = 0; i < array.length; i++) {
      array[i].r = array[i].a + array[i].b;
    }
  • foreach

     
    sum = x => x.r = x.a + x.b;
    array.forEach(sum);
  • for..of with reassign

     
    for (let x of array) {
      x.r = x.a + x.b;
    }
  • for..of with no reassign

     
    const result = [];
    for (let x of array) {
      result.push(x.a + x.b);
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    for
    foreach
    for..of with reassign
    for..of with no reassign

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 6 days ago)
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Firefox 135 on Linux
View result in a separate tab
Test name Executions per second
for 165.1 Ops/sec
foreach 60.7 Ops/sec
for..of with reassign 90.2 Ops/sec
for..of with no reassign 41.9 Ops/sec