从 Array() 中间删除一个条目的最快方法

发布于 2024-07-15 03:09:17 字数 137 浏览 6 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

香橙ぽ 2024-07-22 03:09:18

没有任何基准来支持这一点,但人们会假设本机 Array.splice 方法将是最快的...

因此,要删除索引 5 处的条目:

array.splice(5, 1);

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:

array.splice(5, 1);
ぃ弥猫深巷。 2024-07-22 03:09:18

如果您不关心数组中项目的顺序(只是希望它短 1),您可以将数组的最后一个元素复制到要删除的索引,然后 pop 最后一个元素关闭。

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

array[index] = array[array.length-1];
array.pop();

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

清欢 2024-07-22 03:09:18

Array.splice() " 添加元素到数组并从数组中删除元素”

myArr.splice(indexToRemove, 1); // only removing one index, thus the 1

Array.splice() "adds elements to and removes elements from an array":

myArr.splice(indexToRemove, 1); // only removing one index, thus the 1
娜些时光,永不杰束 2024-07-22 03:09:18

我测试了 Array.prototype.splice() 并发现它在大型数组上非常慢。

删除元素的一种更快的方法是将要保留的元素复制到新数组中,同时跳过要删除的元素。 完成复制后,您只需用新数组覆盖旧数组即可。

在我的测试中,我从包含 100.000 个项目的数组中删除了所有其他元素。 该测试将 Array.prototype.splice() 与其他方法进行了比较。 结果如下:

855 ms = splice
  7 ms = manual copying without preserving the original array
 14 ms = manual copying with preserving the original array

这是最后一个方法的代码:

var arrB = [],
    i=varA.length,
    j=0;

// copy even items to a new array
while(i > 0) {
    i-=2; // skip two elements
    arrB[j++] = arrA[i];
}

// clear the old array
arrA.splice(0, arrA.length);

// copy values back to the old array
// array is preserved (references to the array don't need to be updated)
arrA.push.apply(arrA, arrB);

实际测试可以在 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:

855 ms = splice
  7 ms = manual copying without preserving the original array
 14 ms = manual copying with preserving the original array

Here's the code for the last method:

var arrB = [],
    i=varA.length,
    j=0;

// copy even items to a new array
while(i > 0) {
    i-=2; // skip two elements
    arrB[j++] = arrA[i];
}

// clear the old array
arrA.splice(0, arrA.length);

// copy values back to the old array
// array is preserved (references to the array don't need to be updated)
arrA.push.apply(arrA, arrB);

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/

黯淡〆 2024-07-22 03:09:18

根据您的情况,如果您想优先考虑性能,可以考虑使用字典而不是数组。

var dict:Dictionary = new Dictionary();

// The following value/key set should be customized so you can 
// get use of them in your specific case.

dict[item1] = item1;
dict[item2] = item2;

...

delete dict[item1];

Depending on your case, you may consider using a Dictionary instead of an Array if you want to prioritize the performance.

var dict:Dictionary = new Dictionary();

// The following value/key set should be customized so you can 
// get use of them in your specific case.

dict[item1] = item1;
dict[item2] = item2;

...

delete dict[item1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文