new tips

老外的一篇文章(原文地址http://stackoverflow.com/questions/6647677/tips-for-efficient-as3-coding),有这么一段描述:

Use [] and new Object(), instead of, new Array() and {}. It is at best 3 times faster. And is extremely common for the more costly operation to occur in loops.

Generally speaking, the new keyword is just plain expensive.

import flash.utils.getTimer;publicvar _time:Number;publicvar _simpleArrayTime:Number;publicvar buffer:Array;publicfunction testArray():void{
    trace("----------------Array Test--------------");
    _time = getTimer();for(var a:int=0; a <100000; a++)
        buffer =[];
    _simpleArrayTime = getTimer()- _time;
    trace("[] * 100000 :"+_simpleArrayTime.toPrecision(21));

    _time = getTimer();for(var b:int=0; b <100000; b++)
        buffer =newArray();
    _simpleArrayTime = getTimer()- _time;
    trace("new Array() * 100000 :"+_simpleArrayTime.toPrecision(21));

    _time = getTimer();for(var c:int=0; c <100000; c++)
        buffer =[];
    _simpleArrayTime = getTimer()- _time;
    trace("[] * 100000 :"+_simpleArrayTime.toPrecision(21));}publicvar objBuffer:Object;publicfunction testObject():void{
    trace("----------------Object Test--------------");
    _time = getTimer();for(var a:int=0; a <100000; a++)
        objBuffer ={};
    _simpleArrayTime = getTimer()- _time;
    trace("{} * 100000 :"+_simpleArrayTime.toPrecision(21));

    _time = getTimer();for(var b:int=0; b <100000; b++)
        objBuffer =newObject();
    _simpleArrayTime = getTimer()- _time;
    trace("new Object() * 100000 :"+_simpleArrayTime.toPrecision(21));

    _time = getTimer();for(var c:int=0; c <100000; c++)
        objBuffer ={};
    _simpleArrayTime = getTimer()- _time;
    trace("{} * 100000 :"+_simpleArrayTime.toPrecision(21));}publicfunction runTests(event:Event=null):void{
    testArray();
    testObject();

}

----------------Array Test--------------
[] * 100000 :82.0000000000000000000
new Array() * 100000 :152.000000000000000000
[] * 100000 :53.0000000000000000000
----------------Object Test--------------
{} * 100000 :53.0000000000000000000
new Object() * 100000 :36.0000000000000000000
{} * 100000 :53.0000000000000000000



但在我本地的三次测试结果是这样的(WIN7+FB4.6+SDK4.6):


----------------Array Test--------------
[] * 100000 :42.0000000000000000000
new Array() * 100000 :117.000000000000000000
[] * 100000 :43.0000000000000000000
----------------Object Test--------------
[SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
{} * 100000 :31.0000000000000000000
new Object() * 100000 :30.0000000000000000000
{} * 100000 :30.0000000000000000000

----------------Array Test--------------
[] * 100000 :46.0000000000000000000
new Array() * 100000 :124.000000000000000000
[SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
[] * 100000 :46.0000000000000000000
----------------Object Test--------------
{} * 100000 :27.0000000000000000000
new Object() * 100000 :29.0000000000000000000
{} * 100000 :28.0000000000000000000

----------------Array Test--------------
[] * 100000 :45.0000000000000000000
new Array() * 100000 :112.000000000000000000
[] * 100000 :43.0000000000000000000
----------------Object Test--------------
[SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
{} * 100000 :28.0000000000000000000
new Object() * 100000 :28.0000000000000000000
{} * 100000 :30.0000000000000000000

所以说,[]比new Array要快,但{}并不比new Object快!

原文地址:https://www.cnblogs.com/tianlanliao/p/3434468.html