Skip to content

Commit b7fd55e

Browse files
committed
events: speed up newListener/removeListener events
1 parent 84221fd commit b7fd55e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/events.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ EventEmitter.prototype.addListener = function(type, listener) {
133133

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

139141
if (!this._events[type]) {
140142
// Optimize the case of one listener. Don't need the extra array object.
@@ -217,12 +219,18 @@ EventEmitter.prototype.removeListener = function(type, listener) {
217219
list.splice(position, 1);
218220
if (list.length == 0)
219221
delete this._events[type];
220-
this.emit('removeListener', type, listener);
222+
223+
if (this._events.removeListener) {
224+
this.emit('removeListener', type, listener);
225+
}
221226
} else if (list === listener ||
222227
(list.listener && list.listener === listener))
223228
{
224229
delete this._events[type];
225-
this.emit('removeListener', type, listener);
230+
231+
if (this._events.removeListener) {
232+
this.emit('removeListener', type, listener);
233+
}
226234
}
227235

228236
return this;

0 commit comments

Comments
 (0)