Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Chrome 130
Windows
Desktop
6 months ago
Test name Executions per second
foreach 6.5 Ops/sec
for-in 3.6 Ops/sec
for-of 7.1 Ops/sec
for 4.7 Ops/sec
Tests:
  • foreach

    x
     
    const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40));
    let index = 0;
    items.forEach(i => index++);
  • for-in

     
    const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40));
    let index = 0;
    for (let i in items) {
        item = items[i]
        index++;
    }
  • for-of

     
    const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40));
    let index = 0;
    for (let i of items) {
        index++;
    }
  • for

     
    const items = Array.from({length: 500000}, () => Math.floor(Math.random() * 40));
    let index = 0;
    for(let i = 0; i < items.length; ++i) {
        item = items[i]
      index++;
    }