Run details:
Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Firefox 128
Linux
Desktop
2 months ago
Test name Executions per second
For 38.1 Ops/sec
For...Of 32.8 Ops/sec
ForEach 31.5 Ops/sec
Script Preparation code:
AخA
 
function generateTestArray() {
    const result = [];
    for (let i = 0; i < 1000000; ++i) {
        result.push({
            a: i,
            b: i / 2,
            x: 0,
            y: 0,
        });
    }
    return result;
}
Tests:
  • For

     
    const array = generateTestArray();
    for (let i = 0; i < array.length; i++) {
        array[0].x = array[0].a + array[0].b;
        array[0].y = array[0].a + array[0].b * 2;
    }
  • For...Of

     
    const array = generateTestArray();
    for (const a of array) {
        a.x = a.a + a.b;
        a.y = a.a + a.b * 2;
    }
  • ForEach

     
    const array = generateTestArray();
    array.forEach(a => {
      a.x = a.a + a.b;
      a.y = a.a + a.b * 2;
    });