/**
* Copyright _katsuren ( http://wonderfl.net/user/_katsuren )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/kdMK
*/
package {
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.EventDispatcher;
public class FlashTest extends Sprite {
public static const TEST_COUNT:int = 1000000;
private var _tf:TextField = new TextField();
private var _dispatcher:EventDispatcher;
private var _callback:Callback;
private var _callback2:Callback2;
private var _callback3:Callback3;
private var _count:int = 0;
public function FlashTest() {
_tf.x = 0;
_tf.y = 0;
_tf.width = 150;
_tf.height = 200;
addChild(_tf);
_dispatcher = new EventDispatcher();
_dispatcher.addEventListener("test", onEvent);
_callback = new Callback(onCallback);
_callback2 = new Callback2();
_callback2.addCallback("test", onCallback);
_callback3 = new Callback3();
_callback3.addCallback("test", onCallback);
stage.frameRate = 1;
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(e:Event):void {
if (_count == 0)
testEventDispatcher();
else if (_count == 1)
testEventDispatcher2();
else if (_count == 2)
testCallback();
else if (_count == 3)
testCallback2();
else if (_count == 4)
testCallback3();
_count++;
}
public function testEventDispatcher():void {
var start:Number = new Date().getTime();
for (var i:int=0; i<TEST_COUNT; i++) {
_dispatcher.dispatchEvent(new Event("test"));
}
var end:Number = new Date().getTime();
var time:String = (end-start).toString();
appendText("Event takes : " + time + "ms");
}
public function onEvent(e:Event):void {}
public function testEventDispatcher2():void {
var e:Event = new Event("test");
var start:Number = new Date().getTime();
for (var i:int=0; i<TEST_COUNT; i++) {
_dispatcher.dispatchEvent(e);
}
var end:Number = new Date().getTime();
var time:String = (end-start).toString();
appendText("Event2 takes : " + time + "ms");
}
public function testCallback():void {
var start:Number = new Date().getTime();
for (var i:int=0; i<TEST_COUNT; i++) {
_callback.call();
}
var end:Number = new Date().getTime();
var time:String = (end-start).toString();
appendText("Callback takes : " + time + "ms");
}
public function onCallback():void {}
public function testCallback2():void {
var start:Number = new Date().getTime();
for (var i:int=0; i<TEST_COUNT; i++) {
_callback2.call("test");
}
var end:Number = new Date().getTime();
var time:String = (end-start).toString();
appendText("Callback2 takes : " + time + "ms");
}
public function testCallback3():void {
var start:Number = new Date().getTime();
for (var i:int=0; i<TEST_COUNT; i++) {
_callback3.call("test");
}
var end:Number = new Date().getTime();
var time:String = (end-start).toString();
appendText("Callback3 takes : " + time + "ms");
}
public function appendText(str:String):void {
_tf.appendText(str+"\n");
}
}
}
class Callback {
private var _func:Function = null;
public function Callback(func:Function):void {
_func = func;
}
public function call() :void { _func(); }
}
class Callback2 {
private var _map:Object = {};
public function Callback2() {}
public function addCallback(type:String, func:Function):void {
if (_map[type] == undefined)
_map[type] = new Vector.<Function>();
_map[type].push(func);
}
public function call(type:String):void {
try {
var list:Vector.<Function> = _map[type] as Vector.<Function>;
var length:uint = list.length;
}catch(err:Error){}
for (var i:int=0; i<length; i++) {
list[i]();
}
}
}
class Callback3 {
private var _map:Object = {};
public function Callback3() {}
public function addCallback(type:String, func:Function):Boolean {
if (_map[type] == undefined)
_map[type] = new Vector.<Function>();
_map[type].push(func);
return true;
}
public function removeCallback(type:String, func:Function):Boolean {
try {
var list:Vector.<Function> = _map[type] as Vector.<Function>;
var length:uint = list.length;
}catch(err:Error){ return false; }
for (var i:int=0; i<length; i++) {
if (list[i] == func) {
list.splice(i, 1);
return true;
}
}
return false;
}
public function call(type:String):void {
try {
var list:Vector.<Function> = _map[type] as Vector.<Function>;
var length:uint = list.length;
}catch(err:Error){}
for (var i:int=0; i<length; i++) {
list[i]();
}
}
public function hasCallback(type:String):Boolean {
try {
var list:Vector.<Function> = _map[type] as Vector.<Function>;
var length:uint = list.length;
}catch(err:Error) { return false; }
if (length>0)
return true;
return false;
}
public function willTrigger(type:String):Boolean {
return hasCallback(type);
}
}