js push vs pre-alloc

Benchmark created on


Setup

const NUMBER_OF_ELEMENTS = 10**8;
const ELEMENT_LEN_BYTES = 4;

Test runner

Ready to run.

Testing in
TestOps/sec
push
const array = []

console.time('array')

for (let i = 1; i <= NUMBER_OF_ELEMENTS; i++) {
    array.push(i)
}

console.timeEnd('array')
ready
alloc
const preAllocatedArray = new Array(NUMBER_OF_ELEMENTS)

console.time('pre-allocated array')

for (let i = 1; i <= NUMBER_OF_ELEMENTS; i++) {
    preAllocatedArray[i - 1] = i
}

console.timeEnd('pre-allocated array')
ready

Revisions

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