开发者

faster array cleaning method in as3

开发者 https://www.devze.com 2023-03-13 06:57 出处:网络
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 p

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消