faster array cleaning method in as3
What's faster for cleaning an array in actionscript 3?
myArray = [];
or
myArray.length = 0;
and why is faster? exist a better method than these? 开发者_开发知识库...
I wrote the following test program:
var array:Array = [];
var start:int = getTimer();
for (var index:int = 0; index < 10000; index++)
{
array.push(4);
array = [];
//array.length = 0;
}
var end:int = getTimer();
trace (end - start);
Using .length = 0 reports 10 milliseconds. Using array = [] reports 21 milliseconds. Clearly, doing .length = 0 is much faster. Additionally, doing array = [] may lead to earlier/more frequent garbage collections as it is probably performing a heap allocation. Garbage collection slows down the application at a later time.
.length = 0 wins for multiple reasons.
精彩评论