Here is a possible explanation of the Javascript delete
operator (as used on an Array):
For Javascript arrays, the delete
operation is O(1)
, because it just mutates the value of a random access array to undefined
.
The operation which would reorganize the indices of the array like so (in pseudo javascript):
a = [1,2,3]
-> delete a[1]
-> a is now [1,3]
would be O(N)
because in the worst case, you would delete the first element and the rest of the array would have to be shifted.
See http://en.wikipedia.org/wiki/Array_data_structure and read the part under the header Efficiency.
Addition:
Another explanation of the behaviour of the delete
operator on arrays is so that the delete
operator on arrays does the same thing as the delete
operator on other objects.
If I have a given object: a = {'hi': 'hello', 'bye': 'good bye'}
and I delete a['hi']
then a['hi']
becomes undefined
.
If I have an array arry = [1,2,3]
, then delete arry[1]
then arry[1]
also becomes undefined
.