mroderick/PubSubJS — Issue #3: Speed up when events spawn events
Author: grignaak
Created: 2012-02-02 08:00:28+00:00
State: open

There's a simple way to increase the throughput of asynchronous events when they spawn each other.

The idea is to only have the _first_ call to publish() call setTimeout, which creates an event pump for all the events that the first event spawned. These subsequent events get added to a queue, which the event pump drains. When the event queue is empty, the pump exits (returns in javascript), and the next event spawns a new event pump.

I've coded it up:

**jsperf (unpublished):** http://jsperf.com/pubsubjs-vs-jquery-custom-events/17

**code:** https://gist.github.com/1715452

(I also cleaned up the code a bit, but didn't have access to all the tests, which is why I didn't do this as a pull request.)