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

Type casting comparison

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

package {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	[SWF(width = "240",height = "180")]
	public class TypeCasting extends Sprite {
		private const COUNT:int = 10000000;
		private var _mc:Object = new MovieClip();
		private var started:uint;
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		public function TypeCasting() {
			// Creating a TextField for display
			createTextField();
			// Starting test
			cast();
			as_operator();
			untyped();
		}
		private function cast():void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				var test:MovieClip = MovieClip(_mc);
			}
			xTrace(getTimer() - started);
		}
		private function as_operator():void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				var test:MovieClip = _mc as MovieClip;
			}
			xTrace(getTimer() - started);
		}
		private function untyped():void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				var temp:* = _mc;
				var test:MovieClip = temp;
			}
			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 = "cast:\nas operator:\nuntyped:";
		}
		private function xTrace(n:int):void {
			my_txt.appendText(String(n) + "\n");
		}
	}
}