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

     
    const array = generateTestArray();
    array.forEach((x) => {
       x.r = x.a + x.b;
    });
  • for

     
    const array = generateTestArray();
    for(let index = 0; index < array.length; index++) {
        array[index].r = array[index].a + array[index].b;
    }
  • for name first

     
    const array = generateTestArray();
    for(let index = 0; index < array.length; index++) {
        const value = array[index]
        value.r = value.a + value.b;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    .forEach
    for
    for name first

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Chrome 114 on Windows
View result in a separate tab
Test name Executions per second
.forEach 14.3 Ops/sec
for 21.2 Ops/sec
for name first 21.0 Ops/sec