Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
events: stricter prop & variable checks for perf
Browse files Browse the repository at this point in the history
Replace truthy/falsey checks of _events and _events[type] with
comparisons to undefined for better performance:

events/ee-add-remove.js n=250000    5.30 %    *** 4.260028e-07
events/ee-emit.js n=2000000         4.18 %    *** 1.026649e-05

This has a knock-on effect on modules that use lots of events, e.g.:

http2/headers.js nheaders=0 n=1000  2.60 %    *** 0.000298338

PR-URL: nodejs/node#16212
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
apapirovski authored and addaleax committed Dec 7, 2017
1 parent 7d29d4a commit 88b62b7
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/extras/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
EventEmitter.init = function() {
this.domain = null;

if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
if (this._events === undefined ||
this._events === Object.getPrototypeOf(this)._events) {
this._events = Object.create(null);
this._eventsCount = 0;
}
Expand Down Expand Up @@ -165,8 +166,8 @@ EventEmitter.prototype.emit = function emit(type) {
var doError = (type === 'error');

events = this._events;
if (events)
doError = (doError && events.error == null);
if (events !== undefined)
doError = (doError && events.error === undefined);
else if (!doError)
return false;

Expand All @@ -176,7 +177,7 @@ EventEmitter.prototype.emit = function emit(type) {
if (doError) {
if (arguments.length > 1)
er = arguments[1];
if (domain) {
if (domain !== null && domain !== undefined) {
if (!er) {
er = new Error('Unhandled "error" event');
er.code = 'ERR_UNHANDLED_ERROR';
Expand All @@ -202,7 +203,7 @@ EventEmitter.prototype.emit = function emit(type) {

handler = events[type];

if (!handler)
if (handler === undefined)
return false;

if (typeof process !== 'undefined' && domain && this !== process) {
Expand Down Expand Up @@ -254,13 +255,13 @@ function _addListener(target, type, listener, prepend) {
}

events = target._events;
if (!events) {
if (events === undefined) {
events = target._events = Object.create(null);
target._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (events.newListener) {
if (events.newListener !== undefined) {
target.emit('newListener', type,
listener.listener ? listener.listener : listener);

Expand All @@ -271,7 +272,7 @@ function _addListener(target, type, listener, prepend) {
existing = events[type];
}

if (!existing) {
if (existing === undefined) {
// Optimize the case of one listener. Don't need the extra array object.
existing = events[type] = listener;
++target._eventsCount;
Expand Down Expand Up @@ -390,11 +391,11 @@ EventEmitter.prototype.removeListener =
}

events = this._events;
if (!events)
if (events === undefined)
return this;

list = events[type];
if (!list)
if (list === undefined)
return this;

if (list === listener || list.listener === listener) {
Expand Down Expand Up @@ -427,7 +428,7 @@ EventEmitter.prototype.removeListener =
if (list.length === 1)
events[type] = list[0];

if (events.removeListener)
if (events.removeListener !== undefined)
this.emit('removeListener', type, originalListener || listener);
}

Expand All @@ -439,15 +440,15 @@ EventEmitter.prototype.removeAllListeners =
var listeners, events, i;

events = this._events;
if (!events)
if (events === undefined)
return this;

// not listening for removeListener, no need to emit
if (!events.removeListener) {
if (events.removeListener === undefined) {
if (arguments.length === 0) {
this._events = Object.create(null);
this._eventsCount = 0;
} else if (events[type]) {
} else if (events[type] !== undefined) {
if (--this._eventsCount === 0)
this._events = Object.create(null);
else
Expand Down Expand Up @@ -475,7 +476,7 @@ EventEmitter.prototype.removeAllListeners =

if (typeof listeners === 'function') {
this.removeListener(type, listeners);
} else if (listeners) {
} else if (listeners !== undefined) {
// LIFO order
for (i = listeners.length - 1; i >= 0; i--) {
this.removeListener(type, listeners[i]);
Expand All @@ -490,11 +491,11 @@ EventEmitter.prototype.listeners = function listeners(type) {
var ret;
var events = this._events;

if (!events)
if (events === undefined)
ret = [];
else {
evlistener = events[type];
if (!evlistener)
if (evlistener === undefined)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener.listener || evlistener];
Expand All @@ -517,12 +518,12 @@ EventEmitter.prototype.listenerCount = listenerCount;
function listenerCount(type) {
const events = this._events;

if (events) {
if (events !== undefined) {
const evlistener = events[type];

if (typeof evlistener === 'function') {
return 1;
} else if (evlistener) {
} else if (evlistener !== undefined) {
return evlistener.length;
}
}
Expand Down

0 comments on commit 88b62b7

Please sign in to comment.