package { import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; import flash.utils.getTimer; public class EventsVsFunctions extends Sprite { public function EventsVsFunctions() { const NUM_ITERATIONS:int = 100000; var startTime:int, stopTime:int; var eventsDuration:int, functionsDuration:int; var event:Event = new Event(Event.CHANGE); var i:int; // setup event listeners addEventListener(Event.CHANGE, onChange1); addEventListener(Event.CHANGE, onChange2); addEventListener(Event.CHANGE, onChange3); // time event dispatchs startTime = getTimer(); for (i; i < NUM_ITERATIONS; i++) dispatchEvent(event); stopTime = getTimer(); eventsDuration = stopTime - startTime; // setup function listeners var listeners:Array = [ onChange1, onChange2, onChange3 ]; var listener:Function; // time function calls startTime = getTimer(); for (i = 0; i < NUM_ITERATIONS; i++) for each (listener in listeners) listener(event); stopTime = getTimer(); functionsDuration = stopTime - startTime; // output results var tf:TextField = new TextField(); tf.autoSize = "left"; tf.text = "Events Duration: " + eventsDuration + " ms\n" + "Functions Duration: " + functionsDuration + " ms"; addChild(tf); } // do nothing functions -- mxmlc won't dead-strip empty functions, so they're fine private function onChange1(event:Event):void { } private function onChange2(event:Event):void { } private function onChange3(event:Event):void { } } }