Reputation: 89753
I want to remove an item from an array, is array.splice(x,1)
the best way to go?
Is array.splice(x,1)
functionally equivalent to delete array[x]; array.length-=1
?
I've read these threads: Javascript object with array, deleting does it actually remove the item? and Deleting array elements in JavaScript - delete vs splice
and did some tests:
<script>
var arr=[];
for(var x=0;x<100000;++x){
arr.push(x);
}
var a=new Date().getTime();
for(var x=0;x<50000;++x){
arr.splice(49999,1);
}
var b=new Date().getTime();
alert(b-a);
</script>
<script>
var arr=[];
for(var x=0;x<100000;++x){
arr.push(x);
}
var a=new Date().getTime();
for(var x=0;x<50000;++x){
delete arr[49999];
arr.length-=1;
}
var b=new Date().getTime();
alert(b-a);
</script>
The timing difference is over a magnitude of 100, making the itch to use the second solution almost irresistable.. but before using it, I would like to ask this question: are there any traps i should look out for when i use delete array[x]; array.length-=1
instead of array.splice(x,1)
?
Upvotes: 1
Views: 2321
Reputation: 344713
If you're just lopping off the last element in the array, you can use pop()
and throw away the result or just decrement the length by 1. The delete
operator isn't even required here, and splice()
is more appropriate for other uses.
Specifically, section 15.4 of the ECMAScript specification says:
whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted.
Both methods mentioned are outlined at MDC:
Either are appropriate for your situation - by all means modify length
if you get better performance from it.
Upvotes: 3
Reputation: 122966
Using delete
does not delete the element. After delete somearr[n]
somearr[n]
still exists but its value is undefined
. There are a few ways to remove elements from arrays.
Array.splice
Array.pop()
or maybe Array.length = Array.length-1
Array.shift()
or Array.slice(1)
To be complete, using Array.slice
you could make up a function too:
function deleteElementsFromArray(arr,pos,n){
return arr.slice(0,pos).concat(arr.slice(pos+n));
}
Upvotes: 2
Reputation: 385325
array.splice
may perform other internal operations relevant to the splice.
Your delete array[x]; array.length-=1
just hacks around the public interface and assumes that there's nothing else to do internally.
In fact, this is the cause of the timing difference: there is plenty more to do internally in order to actually splice the array.
Use the proper interface. That's why it's there.
Upvotes: 2
Reputation: 46647
Deleting array elements just sets them to undefined
- that's why it is so fast. It does not remove the element. Decreasing the length of the array makes it even worse as the array length doesn't change at all!
In other words: the response to your question title is no and you should use splice()
to achieve the intended effect. You can use the delete 'trick' to achieve greater performance only if your code handles the possibility of undefined
elements. That can be useful, but it has nothing to do with 'removing an item from an array'.
Upvotes: 1