简体   繁体   中英

should i use `delete array[x]; array.length-=1` instead of `array.splice(x,1)`?

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 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 delete array[x]; array.length-=1 instead of array.splice(x,1) ?

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.

array.splice may perform other internal operations relevant to the splice.

Your delete array[x]; array.length-=1 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.

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.

  • within an array for one ore more elements: Array.splice
  • from the end of an array (ie the last element): Array.pop() or maybe Array.length = Array.length-1
  • from the beginning of an array (ie the first element): 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));
}

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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM