immutable copy array prepend lofi

Benchmark created on


Setup

const numbers = Array.from({length: 100000}, ()=> Math.floor(Math.random() * 100));

Test runner

Ready to run.

Testing in
TestOps/sec
Prepend slice, splice
const previous = numbers.pop();
const result1 = [...numbers];
result1.splice(0, 0, previous );

ready
Prepend slice, spread
const first = numbers[numbers.length-1];
const slice = Array.prototype.slice.call(numbers, 0, numbers.length-2);
const result2 = [first , ...slice];

ready
Prepend
const result3 = [numbers[numbers.length-1]];
const result31 = result3.concat(Array.prototype.slice.call(numbers, 0, numbers.length-2));
ready
prepend then push
const result4 = [];
result4.push(numbers[numbers.length-1])
for(let i = 0; i < numbers.length-1; i++){
	result4.push(numbers[i]);
}
ready
unshift
const result5 = Array.prototype.slice.call(numbers, 0, numbers.length-2);
result5.unshift(numbers[numbers.length-1]);
ready
Prepend slice, spread 2
const result6 = [numbers[numbers.length-1], ...(Array.prototype.slice.call(numbers, 0, numbers.length-2))];
ready
pop spread
const previous = numbers.pop();
const result7 = [previous, ...numbers];
ready
pop spread unshift
const previous = numbers.pop();
const result8 = [...numbers];
result8.unshift(previous );
ready

Revisions

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