There is a fallacy in that benchmark: .splice
preserves the order of the elements in the array, and therefore needs to move half of the elements until the hole created by the removal is sifted up to the end and can be removed by resizing the array. It follows that .splice
works in linear time.
Conversely, this piece of code:
array[500000] = array[array.length-1];
array.pop();
swaps the last element with the one to be removed, and shortens the array of 1 element, an operation that can be done in constant time. Technically, the snippet above does not even accomplish the declared goal, since it changes the order of elements in the array (!). Compare:
> array.splice(500000,1)
> console.log(array[500000])
500001
with:
> array[500000] = array[array.length-1];
> array.pop();
> console.log(array[500000])
999999