从 Array() 中间删除一个条目的最快方法
从 Array() 中间删除一个特定条目的最快方法是什么?
数组是一个包含字符串的大数组。
我不想只设置 Array[5] = null,而是数组大小应该减少一,并且 array[5] 应该包含 array[6] 等的内容。
What is the fastest way to delete one specific entry from the middle of Array()
Array is large one having Strings.
I dont want just to set Array[5] = null, but instead array size should be reduced by one and array[5] should have content of array[6] etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有任何基准来支持这一点,但人们会假设本机 Array.splice 方法将是最快的...
因此,要删除索引 5 处的条目:
Don't have any benchmarks to support this, but one would assume that the native Array.splice method would be the fastest...
So, to remove the entry at index 5:
如果您不关心数组中项目的顺序(只是希望它短 1),您可以将数组的最后一个元素复制到要删除的索引,然后 pop 最后一个元素关闭。
我猜想,如果你可以重新排序数组,那么从 CPU 时间角度来看,这会更快。
编辑:您应该针对您的具体情况进行基准测试; 我最近这样做了,直接拼接速度更快。 (大概是因为 Chrome 实际上并未将数组存储为单个连续缓冲区。)
If you don't care about the order of the items in the array (but just want it to get 1 shorter) you can copy the last element of the array to the index to be deleted, then pop the last element off.
I would guess this is faster, CPU-time-wise, if you can get away with reordering the array.
EDIT: You should benchmark for your specific case; I recently did this, and it was faster to just splice. (Presumably because Chrome is not actually storing the array as a single continuous buffer.)
Array.splice() " 添加元素到数组并从数组中删除元素”:
Array.splice() "adds elements to and removes elements from an array":
我测试了 Array.prototype.splice() 并发现它在大型数组上非常慢。
删除元素的一种更快的方法是将要保留的元素复制到新数组中,同时跳过要删除的元素。 完成复制后,您只需用新数组覆盖旧数组即可。
在我的测试中,我从包含 100.000 个项目的数组中删除了所有其他元素。 该测试将 Array.prototype.splice() 与其他方法进行了比较。 结果如下:
这是最后一个方法的代码:
实际测试可以在 jsFiddle 上找到: http ://jsfiddle.net/sansegot/eXvgb/3/
如果您只需要删除一些项目,结果会有很大不同 - 在这种情况下 Array.prototype.splice() 更快(尽管差异并不大)这么大)! 仅当您需要多次调用 splice() 时,才值得实现自定义算法。
第二个测试,其中要删除有限数量的元素可以在此处找到:
http://jsfiddle.net/sansegot/ZeEFJ/1/
I tested Array.prototype.splice() and found that it's very slow on large arrays.
A much faster way of removing elements is to copy the ones you wish to keep to a new array, while skipping the ones you want to remove. After you've finished copying, you simply override the old array with the new one.
In my test I removed every other element from an array containing 100.000 items. The test compared Array.prototype.splice() to other methods. Here are the results:
Here's the code for the last method:
The test in action can be found on jsFiddle: http://jsfiddle.net/sansegot/eXvgb/3/
The results are much different if you only need to remove a few items - in such cases Array.prototype.splice() is faster (although the difference is not so large)! Only if you need to call splice() many times it's worth it to implement custom algorithm.
The second test, in which a limited number of elements are to be removed can be found here:
http://jsfiddle.net/sansegot/ZeEFJ/1/
根据您的情况,如果您想优先考虑性能,可以考虑使用字典而不是数组。
Depending on your case, you may consider using a Dictionary instead of an Array if you want to prioritize the performance.