【问题标题】:Fastest way to delete one entry from the middle of Array()从 Array() 中间删除一个条目的最快方法
【发布时间】:2010-10-12 21:49:11
【问题描述】:

从 Array() 中间删除一个特定条目的最快方法是什么

数组很大,有字符串。

我不想只设置 Array[5] = null,而是应该将数组大小减一,并且 array[5] 应该包含 array[6] 等的内容。

【问题讨论】:

    标签: actionscript-3 apache-flex flex3


    【解决方案1】:

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

    所以,要删除索引 5 处的条目:

    array.splice(5, 1);
    

    【讨论】:

    • splice() 确实是要走的路,但请记住,对于大型数组,删除中间的东西会很慢,因为闪存会“向上移动”后面的条目以填补空白。
    • 另外请记住,除非您向后运行,否则在运行时删除数组中间的东西会造成严重破坏。
    • 对于任何关心被删除的特定条目的值的人,请记住splice 返回一个数组,因此您需要array.splice(5, 1)[0];
    【解决方案2】:

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

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

    我猜这在 CPU 时间方面会更快,如果您可以摆脱对数组的重新排序。

    编辑:您应该针对您的具体情况进行基准测试;我最近这样做了,只是拼接更快。 (大概是因为 Chrome 实际上并没有将数组存储为单个连续缓冲区。)

    【讨论】:

    • 这真是太聪明了。比拼接快得离谱:jsperf.com/remove-element-splice-vs-move-and-pop
    • +1,单行如何:array[index] = array.pop( ) 甚至 array[index] = array[array.length -- -1]
    • @copy,是的,没错,后来我看到了,但是在有多个元素的情况下它是单行的
    • @copy 一个正确的:(array.length > 1) && (array[index] = array.pop()) || array.pop()
    • @Sam 是的,它可能永远都是。 (哦,我当时觉得很聪明……)
    【解决方案3】:

    Array.splice()“在数组中添加元素和从数组中删除元素”

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

    【讨论】:

      【解决方案4】:

      我测试了 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/

      【讨论】:

        【解决方案5】:

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

        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];
        

        【讨论】:

          猜你喜欢
          • 2011-06-28
          • 1970-01-01
          • 2010-11-23
          • 2021-09-16
          • 2010-09-21
          • 1970-01-01
          • 1970-01-01
          • 2020-07-09
          • 1970-01-01
          相关资源
          最近更新 更多