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

Testing addChild() vs addChildAt(0)

package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	[SWF(width = "240",height = "180")]
	public class addChild_vs_addChildAt extends Sprite {
		private var n:int = 10000;
		private var a0:int = 0;
		private var a1:int = 0;
		private var sprite0:Sprite = new Sprite();
		private var sprite1:Sprite = new Sprite();
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		private var my_fmt:TextFormat = new TextFormat();
		public function addChild_vs_addChildAt() {
			// Creating a TextField for display
			createTextField();
			addChild(sprite0);
			addChild(sprite1);
			loopTest();
		}
		function loopTest():void {
			var t0:int = loop0();
			var t1:int = loop1();
			/*
			var _str:String = "";
			_str += "0: " + t0 + ", ";
			_str+="1: "+t1+", ";
			_str+="ratio: "+t0/t1;
			trace(_str);
			*/
			xTrace("addChildAt()", t0);
			xTrace("addChild()", t1);
		}
		function loop0():int {
			var t:int=getTimer();
			for (var i:int=0; i<n; i++) {
				var mySprite:Sprite = new Sprite();
				sprite0.addChildAt(mySprite, 0);
			}
			return getTimer() - t;
		}
		function loop1():int {
			var t:int=getTimer();
			for (var i:int=0; i<n; i++) {
				var mySprite:Sprite = new Sprite();
				sprite1.addChild(mySprite);
			}
			return getTimer() - t;
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_fmt.align = TextFormatAlign.RIGHT;
			my_txt.x +=  50;
			my_txt.defaultTextFormat = my_fmt;
			my_txt.autoSize = TextFieldAutoSize.RIGHT;
			label_txt.autoSize = TextFieldAutoSize.LEFT;
		}
		private function xTrace(_str:String, n:int):void {
			my_txt.appendText(String(n) + "\n");
			label_txt.appendText(_str + ":" + "\n");
		}
	}
}