Squeeze element into the middle of an array (v3)

Revision 3 of this benchmark created on


Setup

let arr = [];
const max = 10000

Test runner

Ready to run.

Testing in
TestOps/sec
using spread syntax
for (let i = 0; i < max; i++) {
	let middle = arr.length >> 1;
	arr = [...arr.slice(0, middle), i, ...arr.slice(middle)];
}
ready
using Array.concat()
for (let i = 0; i < max; i++) {
	let middle = arr.length >> 1;
	arr = arr.slice(0, middle).concat(i, arr.slice(middle));
}
ready
increase array size and use Array.copyWithin()
for (let i = 0; i < max; i++) {
	let middle = arr.length >> 1;
	arr.push(null);
	arr.copyWithin(middle + 1, middle);
	arr[middle] = i;
}
ready
using Array.splice()
for (let i = 0; i < max; i++) {
	let middle = arr.length >> 1;
	arr.splice(middle, 0, i);
}
ready

Revisions

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