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..of

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

     
    const array = generateTestArray();
    const r = [];
    for(const {a, b} of array) {
        r.push(a + b);
    }
  • .map

     
    const array = generateTestArray();
    array.map(x => x.a + x.b)
  • .map (destructuring)

     
    const array = generateTestArray();
    array.map(({a,b}) => a + b)
  • .for (init array)

     
    const array = generateTestArray();
    const r = new Array(array.length);
    for (let i = 0; i < array.length; ++i) {
       r[i] = array[i].a + array[i].b;
    }
  • .reduce

     
    const array = generateTestArray();
    array.reduce((p, x) => p + x.a + x.b, 0);
  • .reduce (destructuring)

     
    const array = generateTestArray();
    array.reduce((p, {a,b}) => p + a + b, 0);
  • for..of (reduce)

     
    const array = generateTestArray();
    let r = 0;
    for (const x of array) {
       r += x.a + x.b;
    }
  • for..of (reduce) (destructuring)

     
    const array = generateTestArray();
    let r = 0;
    for (const {a,b} of array) {
       r += a + b;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    .forEach
    for..of
    for..of (destructuring)
    .map
    .map (destructuring)
    .for (init array)
    .reduce
    .reduce (destructuring)
    for..of (reduce)
    for..of (reduce) (destructuring)

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 hours ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Chrome 134 on Windows
View result in a separate tab
Test name Executions per second
.forEach 19.5 Ops/sec
for..of 35.9 Ops/sec
for..of (destructuring) 23.9 Ops/sec
.map 23.1 Ops/sec
.map (destructuring) 28.9 Ops/sec
.for (init array) 30.1 Ops/sec
.reduce 27.1 Ops/sec
.reduce (destructuring) 26.5 Ops/sec
for..of (reduce) 31.2 Ops/sec
for..of (reduce) (destructuring) 43.0 Ops/sec