Skip to content

Commit

Permalink
events: speed up newListener/removeListener events
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Sep 22, 2012
1 parent 84221fd commit b7fd55e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ EventEmitter.prototype.addListener = function(type, listener) {

// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, typeof listener.listener === 'function' ?
listener.listener : listener);
if (this._events.newListener) {
this.emit('newListener', type, typeof listener.listener === 'function' ?
listener.listener : listener);
}

if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
Expand Down Expand Up @@ -217,12 +219,18 @@ EventEmitter.prototype.removeListener = function(type, listener) {
list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
this.emit('removeListener', type, listener);

if (this._events.removeListener) {
this.emit('removeListener', type, listener);
}
} else if (list === listener ||
(list.listener && list.listener === listener))
{
delete this._events[type];
this.emit('removeListener', type, listener);

if (this._events.removeListener) {
this.emit('removeListener', type, listener);
}
}

return this;
Expand Down

0 comments on commit b7fd55e

Please sign in to comment.