简体   繁体   English

我应该使用`删除数组[x]; array.length-=1`而不是`array.splice(x,1)`?

[英]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?我想从数组中删除一个项目, array.splice(x,1)是 go 的最佳方法吗?

Is array.splice(x,1) functionally equivalent to delete array[x]; array.length-=1 array.splice(x,1)在功能上是否等同于delete array[x]; array.length-=1 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?我已经阅读了这些主题: Javascript object with array,删除它实际上删除了该项目吗? and Deleting array elements in JavaScript - delete vs splice删除 JavaScript 中的数组元素 - 删除与拼接

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时间差超过 100 的幅度,使得使用第二种解决方案的痒几乎无法抗拒.. 但在使用它之前,我想问这个问题:当我使用delete array[x]; array.length-=1 delete array[x]; array.length-=1 instead of array.splice(x,1) ? delete array[x]; array.length-=1而不是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.如果您只是删除数组中的最后一个元素,则可以使用pop()并丢弃结果或将长度减 1。这里甚至不需要delete运算符,而splice()更合适用于其他用途。

Specifically, section 15.4 of the ECMAScript specification says:具体来说,ECMAScript 规范的第 15.4 节说:

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: MDC 概述了提到的两种方法:

Either are appropriate for your situation - by all means modify length if you get better performance from it.两者都适合您的情况 - 如果您从中获得更好的性能,请务必修改length

array.splice may perform other internal operations relevant to the splice. array.splice可以执行与拼接相关的其他内部操作。

Your delete array[x]; array.length-=1你的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. delete array[x]; array.length-=1只是绕过公共接口并假设内部没有其他事情可做。

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.使用delete不会删除元素。 After delete somearr[n] somearr[n] still exists but its value is undefined . delete somearr[n] somearr[n]仍然存在,但它的值是undefined There are a few ways to remove elements from arrays.有几种方法可以从 arrays 中删除元素。

  • within an array for one ore more elements: Array.splice在一个或多个元素的数组中: Array.splice
  • from the end of an array (ie the last element): Array.pop() or maybe Array.length = Array.length-1从数组的末尾(即最后一个元素): Array.pop()或者Array.length = Array.length-1
  • from the beginning of an array (ie the first element): Array.shift() or Array.slice(1)从数组的开头(即第一个元素): Array.shift()Array.slice(1)

To be complete, using Array.slice you could make up a function too:完整地说,使用Array.slice你也可以组成一个 function :

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.删除数组元素只是将它们设置为undefined - 这就是它如此之快的原因。 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.换句话说:对您的问题标题的回答是否定的,您应该使用splice()来达到预期的效果。 You can use the delete 'trick' to achieve greater performance only if your code handles the possibility of undefined elements.只有当您的代码处理undefined元素的可能性时,您才能使用删除“技巧”来获得更高的性能。 That can be useful, but it has nothing to do with 'removing an item from an array'.这可能很有用,但它与“从数组中删除一个项目”无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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