As3 - 如何有效地清除数组?
我一直在寻找清除 ActionScript 3 中的数组。
一些方法建议:array = [];
(内存泄漏?)
其他会说:array.splice(0);
代码>
如果您有其他的,请分享。 哪一种效率更高?
谢谢。
I've been looking to clear an array in ActionScript 3.
Some method suggest : array = [];
(Memory leak?)
Other would say : array.splice(0);
If you have any other, please share.
Which one is the more efficient?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
array.length = 0
或array.splice()
似乎最适合整体性能。array.splice(0);
的执行速度比array.splice(array.length - 1, 1);
array.length = 0
orarray.splice()
seems to work best for overall performance.array.splice(0);
will perform faster thanarray.splice(array.length - 1, 1);
对于具有 100 个元素的数组(基准以毫秒为单位,越低所需时间越少):
For array with 100 elements (benchmarks in ms, the lower the less time needed):
array.pop() 和 array.splice(array.length - 1, 1) 之间有一个关键区别,即 pop 将返回元素的值。在清除数组时,这对于方便的衬垫非常有用,例如:
There is a key difference between array.pop() and array.splice(array.length - 1, 1) which is that pop will return the value of the element. This is great for handy one liners when clearing out an array like:
我想知道,为什么要以这种方式清除数组?清除对该数组的所有引用将使其可用于垃圾回收。如果
array
是对array
的唯一引用,array = []
就会这样做。如果不是,那么您可能不应该清空它(?),还请注意“数组接受字符串作为键”。 splice 和 lenght 都只对整数键进行操作,因此它们对字符串键没有影响。
顺便说一句:
array.splice(array.length - 1, 1);
相当于array.pop();
I wonder, why you want to clear the Array in that manner? clearing all references to that very array will make it available for garbage collection.
array = []
will do so, ifarray
is the only reference to thearray
. if it isn't then you maybe shouldn't be emtpying it (?)also, please note that`Arrays accept Strings as keys. both splice and lenght operate solely on integer keys, so they will have no effect on String keys.
btw.:
array.splice(array.length - 1, 1);
is equivalent toarray.pop();
这对我来说一直很有效,但我还没有机会通过分析器运行它
this has always worked pretty well for me but I haven't had a chance to run it through the profiler yet