代码之家  ›  专栏  ›  技术社区  ›  ALOToverflow

AS3-如何有效地清除阵列?

  •  20
  • ALOToverflow  · 技术社区  · 15 年前

    我一直在寻找清除ActionScript3中的数组。

    一些方法建议: array = []; (内存泄漏?)

    其他人会说: array.splice(0);

    如果你还有其他的,请分享。 哪个效率更高?

    谢谢您。

    5 回复  |  直到 10 年前
        1
  •  29
  •   Jason    15 年前

    array.length = 0 array.splice() 似乎对整体表现最好。

    array.splice(0); 执行速度比 array.splice(array.length - 1, 1);

        2
  •  6
  •   n4pgamer    12 年前

    对于包含100个元素的数组(以毫秒为单位的基准,所需时间越短):

    // best performance (benchmark: 1157)
    array.length = 0;
    // lower performance (benchmark: 1554)
    array = [];
    // even lower performance (benchmark: 3592)
    array.splice(0);
    
        3
  •  2
  •   gregoryb    13 年前

    array.pop()和array.splice(array.length-1,1)之间有一个关键区别,即pop将返回元素的值。这对于清除数组时方便使用的一行程序非常有用,例如:

    while(myArray.length > 0){
         view.removeChild(myArray.pop());
    }
    
        4
  •  2
  •   Taryn Frank Pearson    12 年前

    我想知道,你为什么要这样清除阵列?清除对该数组的所有引用将使其可用于垃圾收集。 array = [] 会这样做,如果 array 是唯一引用 数组 . 如果不是的话,你可能就不该这么做(?)

    另外,请注意,`数组接受字符串作为键。拼接和长度都只在整数键上操作,因此它们对字符串键没有影响。

    顺便说一句。: array.splice(array.length - 1, 1); 等于 array.pop();

        5
  •  1
  •   Sergey Glotov Nitesh Khosla    13 年前
    array.splice(0,array.length);
    

    这对我来说一直都很有效,但我还没有机会通过分析器运行它。