Skip to content

Commit

Permalink
events: speed up .removeAllListeners()
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Sep 22, 2012
1 parent b7fd55e commit 56668f5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ EventEmitter.prototype.removeListener = function(type, listener) {
EventEmitter.prototype.removeAllListeners = function(type) {
if (!this._events) return this;

// fast path
if (!this._events.removeListener) {
if (arguments.length === 0) {
this._events = {};
} else if (type && this._events && this._events[type]) {
this._events[type] = null;
}
return this;
}

// slow(ish) path, emit 'removeListener' events for all removals
if (arguments.length === 0) {
for (var key in this._events) {
if (key === 'removeListener') continue;
Expand Down

0 comments on commit 56668f5

Please sign in to comment.