digitalfruit/limejs — pull request #82 by jmuzz (2013-05-16 17:38:49+00:00)
Title: An optimization for the event dispatcher

I ran into some load time issues while making a map out of clickable tiles and it lead to the discovery of a couple of inefficiency's in the event dispatcher.

- The handler arrays it keeps are sorted every time something is added to it. This wouldn't always be a problem but in my case it isn't necessary since many objects are being added at once without any user interaction.

- The handler arrays are re-sorted every time the DOM tree changes, which means they are unnecessarily sorted twice whenever an event handling node is added. If a branch containing several nodes that handle a particular event type is moved to a different place in the DOM the array for that event type will be sorted again for each of those nodes. It could be redundant because only one sort after the change should be necessary.

The solution I propose is to mark the arrays as unsorted instead of sorting them when the changes happen. They will then get sorted if necessary just before they are accessed. This has made a huge difference in the load time of my game.