For..of vs traditional for loop for copying large amounts of elements

Benchmark created on


Setup

const TOTAL_ELEMS = 48_000_000
let arr = new Array(TOTAL_ELEMS)
let other = new Array(TOTAL_ELEMS)

for (let i = 0; i < TOTAL_ELEMS; i++) {
  arr[i] = i
  other[i] = TOTAL_ELEMS - i
}

Teardown

arr = new Array(TOTAL_ELEMS)
other = new Array(TOTAL_ELEMS)

for (let i = 0; i < TOTAL_ELEMS; i++) {
  arr[i] = i
  other[i] = TOTAL_ELEMS - i
}

Test runner

Ready to run.

Testing in
TestOps/sec
for..of
for (const elem of other) {
  arr.push(elem)
}
ready
traditional for loop
for (let i = 0; i < TOTAL_ELEMS; i++) {
  arr.push(other[i])
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.