In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Initializing Arrays

Get Adobe Flash player
by Fumio 29 Jun 2010
/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/leRa
 */

package {
    import flash.display.Sprite;
    import flash.utils.getTimer;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    [SWF(width = "240",height = "180")]
    public class InitializingArrays extends Sprite {
        private const MAX_NUMBER:int = 1000000;
        private var i:int;
        private var started:int;
        private var my_txt:TextField = new TextField();
        private var label_txt:TextField = new TextField();
        public function InitializingArrays() {
            // Creating a TextField for display
            createTextField();
            // Starting test
            newOperator();
            bracketAccessOperator();
            lengthToZero();
        }
        private function newOperator():void {
            started = getTimer();
            for (i = 0; i < MAX_NUMBER; i++) {
                var my_array:Array = new Array();
                my_array[0] = 1;
            }
            xTrace(getTimer() - started);
        }
        private function bracketAccessOperator():void {
            started = getTimer();
            for (i = 0; i < MAX_NUMBER; i++) {
                var my_array:Array = [];
                my_array[0] = 1;
            }
            xTrace(getTimer() - started);
        }
        private function lengthToZero():void {
            started = getTimer();
            var my_array:Array = new Array();
            for (i = 0; i < MAX_NUMBER; i++) {
                my_array.length = 0;
                my_array[0] = 1;
            }
            xTrace(getTimer() - started);
        }
        private function createTextField():void {
            addChild(my_txt);
            addChild(label_txt);
            my_txt.autoSize = TextFieldAutoSize.RIGHT;
            label_txt.autoSize = TextFieldAutoSize.LEFT;
            label_txt.text = "new:\n[]:\nlength:";
        }
        private function xTrace(n:int):void {
            my_txt.appendText(String(n) + "\n");
        }
    }
}