最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)?

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)?

Share Improve this question edited May 23, 2017 at 10:00 CommunityBot 11 silver badge asked Jun 13, 2011 at 13:34 PacerierPacerier 89.9k111 gold badges385 silver badges645 bronze badges 4
  • 1 The second snippet doesn't do what you want: you're trying to delete element 49999 over and over again, instead of the last element. Moreover, if you want to delete something in the middle of the array, you can't simply delete that item and decrease the length property by 1. – Marcel Korpel Commented Jun 13, 2011 at 13:38
  • @marc you should put that as an answer. it's brilliant – Pacerier Commented Jun 13, 2011 at 14:00
  • 1 No, it's not an answer to your actual question, only a remark. Tomalak's answer is better, so I upvoted him. – Marcel Korpel Commented Jun 13, 2011 at 14:05
  • 1 Your benchmark is flawed – Raynos Commented Jun 13, 2011 at 14:29
Add a ment  | 

4 Answers 4

Reset to default 3

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:

  • Array pop()
  • Array length

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 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 (i.e. the last element): Array.pop() or maybe Array.length = Array.length-1
  • from the beginning of an array (i.e. the first element): Array.shift() or Array.slice(1)

To be plete, 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'.

发布评论

评论列表(0)

  1. 暂无评论