javascript - What's the time complexity of array.splice() in Google Chrome? -
if remove 1 element array using splice() so:
arr.splice(i, 1);
will o(n)
in worst case because shifts elements after i? or constant time, linked list magic underneath?
worst case should o(n)
(copying n-1
elements new array).
a linked list o(1)
single deletion.
for interested i've made lazily-crafted benchmark. (please don't run on windows xp/vista). as can see though, looks constant (i.e. o(1)
), knows they're doing behind scenes make crazy-fast. note regardless, actual splice
fast.
rerunning extended benchmark directly in v8 shell suggest o(n)
. note though need huge array sizes runtime that's affect code. should expected if @ v8 code uses memmove
create new array.
Comments
Post a Comment