flex - Why use IEventDispatcher.hasListener()? -


i'm curious performance benefits of using haseventlistener() on blindly dispatching event?

for example, see in flex framework code snippets such following:

// arraylist.setitemat() var hascollectionlistener:boolean = haseventlistener(collectionevent.collection_change); if (hascollectionlistener) {      dispatchevent(new collectionevent(....)) } 

i assume infers there performance benefits of not dispatching event if no-one listening, versus blindly dispatching it, regardless of whether there listeners attached.

however, doesn't seem make sense.

surely listeners internally stored within sort of performant hashmap. i'd have thought checking see if listener exists performant iterating through empty array of listeners.

therefore, why bother checking, , not blindly invoke dispatchevent method? original code sample clumsier had dispatched event.

regards,

marty

as far can tell, running check redundant. if nobody listening, there nothing in listeners collection, dispatchevent quick... quick asking if there listeners... @ least macro level.

i suppose quick run through profiler tell sure... type of pre-optimization introduces more code , therefore introduces chances of stoopid copy/paste bugs unless know sure there pref problem here , helps relieve it.

there uses haseventlistener call, though. make sense:

if(haseventlistener("heavycomputationchanged")) {     dosomeheavycomputation();     dispatchevent("heavycomputationchanged"); } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -