is supported in part by

July 24, 2008

event-registration performance

In Flash Player 9, the time required to register a listener for a given event increases as the number of listeners already registered for that event increases. Here are the results of a simple event-registration test on a P4-2.6ghz machine running Windows XP:

  • Registering 1000 listeners for an event took .06ms per registration (i.e., an average of .06ms to run addEventListener() once).
  • Registering 20000 listeners for the same event took .47ms per registration.

The increased per-listener registration time results from Flash Player 9's implementation of a safeguard built into the event-registration system: when a listener asks to register for an event, ActionScript first checks whether that listener is already registered; if so, ActionScript rejects the registration request. Listeners are, thus, prevented from registering multiple times for the same event.

In Flash Player 9, the "already-registered" check uses a linear lookup, comparing the new listener to every existing listener before allowing the registration to proceed. If an event already has 20000 listeners, and a new listener asks to register, ActionScript must make 20000 comparisons before the registration can be allowed (hence the high ".47ms per-registration" cost in the test above).

Adobe has improved the situation in Flash Player 10 by using a hash-table lookup instead of a linear lookup when checking for the existence of listeners at registration time. Registering for an event with many existing registered listeners will, therefore, be much faster in Flash Player 10.

Posted by moock at July 24, 2008 08:05 PM