Performance Problem optional arguments

by leichtgewicht
Flash seems to instantiate arrays on every call even unused. Takes away a lot of performance.

There is a good possible bugfix: Vote for:
https://bugs.adobe.com/jira/browse/ASL-224
♥0 | Line 54 | Modified 2011-09-12 22:48:44 | MIT License
play

ActionScript3 source code

/**
 * Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/59ng
 */

package {
    import flash.display.StageScaleMode;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.utils.setTimeout;
    import flash.utils.getTimer;
    final public class FlashTest extends Sprite {
        
        private var _trace: TextField;
        
        public function FlashTest() {
            stage.align ="TL";
            stage.scaleMode=StageScaleMode.NO_SCALE;
            addChild( _trace = new TextField() );
            _trace.width = stage.stageWidth;
            _trace.height = stage.stageHeight;
            trace("Waiting to make sure nothing is distorting the performance test.");
            setTimeout(start, 1000);
        }
        
        public function start():void {
            var t: Number;
            var i:int;
            const count:int = 1000000;
            
            
            
            t = getTimer();
            for( i = 0; i<count;++i){
                a();
            }
            t = getTimer()-t;
            
            trace( t + "ms for: a(...params:Array);" );
            
            t = getTimer();
            for( i = 0; i<count;++i){
                b();
            }
            t = getTimer()-t;
            
            trace( t + "ms for: b(params:Array=null);" );
            
            t = getTimer();
            for( i = 0; i<count;++i){
                c();
            }
            t = getTimer()-t;
            
            trace( t + "ms for: c(...params:Array);" );
        }

        
        public function a(...params:Array):void{
            if(params){}; // if you dont use the variable it will be optimized by the flash player
        }

        public function b(params:Array=null):void{
            if(params){}; // if you dont use the variable it will be optimized by the flash player
        }
        
        public function c(...params:Array):void{
            if(params.length==0){}; // if you dont use the variable it will be optimized by the flash player
        }
        
        
        public function trace( message: String):void {
            _trace.appendText(message+"\n");
        }
    }
}