Javascript Loop Performance - Push Array

Benchmark created on


Setup

const arr = Array.from({length: 100}, () => Math.floor(Math.random() * 100000))
let test = [];

Teardown

test = [];

Test runner

Ready to run.

Testing in
TestOps/sec
for
for (let i = 0; i < arr.length; i++) {
    test.push(arr[i]);
}
ready
for (cached length)
let l = arr.length;
for (let i = 0; i < l; i++) {
    test.push(arr[i]);
}
ready
for (omit init)
let i = 0;
for (; i < arr.length; i++) {
  test.push(arr[i]);
}
ready
for (omit condition)
for (let i = 0;; i++) {
  if (i > arr.length) break;
  test.push(arr[i]);
}
ready
for (omit init & condition)
let i = 0;
for (;; i++) {
  if (i >= arr.length) break;
  test.push(arr[i]);
}
ready
for (omit all 3)
let i = 0;
for (;;) {
  if (i >= arr.length) break;
  test.push(arr[i]);
  i++
}
ready
for ... of
for (const i of arr) {
  test.push(i);
}
ready
for ... in
for (const i in arr) {
  test.push(arr[i]);
}
ready
while
let i = 0;
while(i < arr.length) {
  test.push(arr[i]);
  i++;
}
ready
while (cached length)
let i = 0;
let l = arr.length;
while(i < l) {
  test.push(arr[i]);
  i++;
}
ready
do ... while
let i = 0;
do {
    test.push(arr[i]);
} while(++i < arr.length);
ready
do ... while (cached length)
let i = 0;
let l = arr.length;
do {
    test.push(arr[i]);
} while(++i < l);
ready
forEach
arr.forEach( i => test.push(arr[i]));
ready
map
arr.map(i => test.push(i));
ready
filter
arr.filter(i => test.push(i));
ready
reduce
arr.reduce((a, i) => test.push(i), []);
ready
spread
test.push(...arr);
ready

Revisions

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