How costly is dispatching events using the AS3 event model ?
February 26, 2007 on 8:59 pm | In Actionscript | No CommentsI am currently looking for a simpler and faster framework for my physics engine. The present framework uses events all over the place – even at a very low level for broadcasting internal collision/contact information. With this in mind I tried to figure out how it compares in terms of speed to just invoking the event handler method on the listeners.
If for example 10 objects are constantly in contact, then about 100 events are dispatched every frame, so I thought using events produces a hefty slowdown (registering/removing listeners, creating and populating event object instances…). After running some benchmarks it turned out that the direct invocation is just twice as fast – far less than I thought, which is of course a good thing :-).
By the way, in AS2 the fastest way to dispatch events is using the AsBroadcaster class, and the more listeners you add, the faster it gets compared to direct method invocation.