postaljs/postal.js — pull request #79 by sergiopereiraTT (2014-07-30 18:53:05+00:00)
Title: Cleanup empty channels and topics

This was causing a performance problem in my application. If you look at the code below in `publish()`, in *Api.js*, it loops through all items in the array `this.subscriptions[envelope.channel]`.

```javascript
if (this.subscriptions[envelope.channel]) {
_.each(this.subscriptions[envelope.channel], function (subscribers) {
var idx = 0,
len = subscribers.length,
subDef;
while (idx < len) {
if (subDef = subscribers[idx++]) {
fireSub(subDef, envelope);
}
}
});
}
```

The problem for me was that my code creates *a lot* of one-off topics, which are used in a request-response implementation. The subscriptions are disposed but the _per-topic_ arrays are kept. They are left empty, but they are there. That causes the above loop to get longer and longer.

My proposed fix simply drops these _per-topic_ and _per-channel_ arrays when they are no longer needed.