preallocation vs dynamic allocation

Benchmark created on


Description

I saw a stackoverflow answer saying they were equal performance, and included a link to a testcase, but the dude was so dumb that the testcases didnt even work, so im doing my own testcases here to compare performance

Setup

const length = 1000;

Test runner

Ready to run.

Testing in
TestOps/sec
preallocation 1
let arr = new Array(length);
for(let i = 0;i < length;i++) {
	arr[i] = 1;
}
ready
preallocation 2
let arr = [];
arr.length = length;
for(let i = 0;i < length;i++) {
	arr[i] = 1;
}
ready
dynamic allocation 3
let arr = [];
for(let i = 0;i < length;i++) {
	arr[i] = 1;
}
ready
dynamic allocation 4
let arr = [];
for(let i = 0;i < length;i++) {
	arr.push(1);
}
ready

Revisions

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