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

Commit

Permalink
events: return values directly in listeners
Browse files Browse the repository at this point in the history
Each conditional branch in EventEmitter.prototype.listeners assigns
its return value to a variable ret which is returned at the end.
Instead just return from within each branch. This is both clearer
and more performant.

events/ee-listeners.js n=5000000      3.65 %        *** 3.359171e-10

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 9752299 commit 11b23c8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/extras/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,23 +484,19 @@ EventEmitter.prototype.removeAllListeners =
};

EventEmitter.prototype.listeners = function listeners(type) {
var evlistener;
var ret;
var events = this._events;
const events = this._events;

if (events === undefined)
ret = [];
else {
evlistener = events[type];
if (evlistener === undefined)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener.listener || evlistener];
else
ret = unwrapListeners(evlistener);
}
return [];

return ret;
const evlistener = events[type];
if (evlistener === undefined)
return [];

if (typeof evlistener === 'function')
return [evlistener.listener || evlistener];

return unwrapListeners(evlistener);
};

EventEmitter.listenerCount = function(emitter, type) {
Expand Down

0 comments on commit 11b23c8

Please sign in to comment.