Reduce callbacks immutability: Pre-allocated array accumulator

Benchmark created on


Setup

const maxPower = 7;  
const data = new Array(maxPower+1);
for(i=0; i<=maxPower; i++) {
	const size = 10 ** i;
	const arrI = new Array(size);
	for (j=0; j<size; j++) arrI[j] = j;

	data[i] = arrI;
};

Test runner

Ready to run.

Testing in
TestOps/sec
With pre-allocation
for (i=0; i<=maxPower; i++) {
	const dataI = data[i];
	var result = dataI.reduce(
		(accumulator, item, index) => {
			accumulator[index] = item;
			return accumulator;
		},
		new Array(dataI.length)
	);
}
ready
Without pre-allocation
for (i=0; i<=maxPower; i++) {
	var result = data[i].reduce(
		(accumulator, item, index) => {
			accumulator.push(item);
			return accumulator;
		},
		[]
	);
}
ready

Revisions

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