Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
events: improve event performance
  • Loading branch information
anonrig committed Sep 13, 2022
commit 6395aa610fbce612a69cc20f30df80a3869d2b56
15 changes: 9 additions & 6 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const {
identicalSequenceRange,
} = require('internal/util/inspect');

function NullObject() {}
NullObject.prototype = ObjectCreate(null);

let spliceOne;

const {
Expand Down Expand Up @@ -338,7 +341,7 @@ EventEmitter.init = function(opts) {

if (this._events === undefined ||
this._events === ObjectGetPrototypeOf(this)._events) {
this._events = ObjectCreate(null);
this._events = new NullObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fairly problematic, I think. At the very least it's semver-major. Unfortunately we have a number of ecosystem modules that touch this._events directly and changing the type of thing that is can definitely break stuff.

this._eventsCount = 0;
}

Expand Down Expand Up @@ -547,7 +550,7 @@ function _addListener(target, type, listener, prepend) {

events = target._events;
if (events === undefined) {
events = target._events = ObjectCreate(null);
events = target._events = new NullObject();
target._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
Expand Down Expand Up @@ -685,7 +688,7 @@ EventEmitter.prototype.removeListener =

if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
this._events = new NullObject();
else {
delete events[type];
if (events.removeListener)
Expand Down Expand Up @@ -740,11 +743,11 @@ EventEmitter.prototype.removeAllListeners =
// Not listening for removeListener, no need to emit
if (events.removeListener === undefined) {
if (arguments.length === 0) {
this._events = ObjectCreate(null);
this._events = new NullObject();
this._eventsCount = 0;
} else if (events[type] !== undefined) {
if (--this._eventsCount === 0)
this._events = ObjectCreate(null);
this._events = new NullObject();
else
delete events[type];
}
Expand All @@ -758,7 +761,7 @@ EventEmitter.prototype.removeAllListeners =
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = ObjectCreate(null);
this._events = new NullObject();
this._eventsCount = 0;
return this;
}
Expand Down