What’s the time complexity of array.splice() in Google Chrome?

Worst case should be O(n) (copying all n-1 elements to new array).

A linked list would be O(1) for a single deletion.

For those interested I’ve made this lazily-crafted benchmark. (Please don’t run on Windows XP/Vista). As you can see from this though, it looks fairly constant (i.e. O(1)), so who knows what they’re doing behind the scenes to make this crazy-fast. Note that regardless, the actual splice is VERY fast.

Rerunning an extended benchmark directly in the V8 shell that suggest O(n). Note though that you need huge array sizes to get a runtime that’s likely to affect your code. This should be expected as if you look at the V8 code it uses memmove to create the new array.

Leave a Comment