Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: improve creation time of EventTarget #52410

Closed
wants to merge 1 commit into from
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
17 changes: 9 additions & 8 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class Listener {
}
}

// Updating this function will require updating the EventTarget as well
function initEventTarget(self) {
self[kEvents] = new SafeMap();
self[kMaxEventTargetListeners] = EventEmitter.defaultMaxListeners;
Expand All @@ -554,9 +555,12 @@ class EventTarget {
// Ref: https://github.com/nodejs/node/pull/33661
static [kIsEventTarget] = true;

constructor() {
initEventTarget(this);
}
// From here: Same as initEventTarget code, added here due to performance reasons
[kEvents] = new SafeMap();
[kMaxEventTargetListeners] = EventEmitter.defaultMaxListeners;
[kMaxEventTargetListenersWarned] = false;
[kHandlers] = new SafeMap();
// Until here

[kNewListener](size, type, listener, once, capture, passive, weak) {
if (this[kMaxEventTargetListeners] > 0 &&
Expand Down Expand Up @@ -868,6 +872,8 @@ ObjectDefineProperties(EventTarget.prototype, {
},
});

// If you update this function please add to node event target as well,
// not used in NodeEventTarget due to performance reasons
function initNodeEventTarget(self) {
initEventTarget(self);
}
Expand All @@ -876,11 +882,6 @@ class NodeEventTarget extends EventTarget {
static [kIsNodeEventTarget] = true;
static defaultMaxListeners = 10;

constructor() {
super();
initNodeEventTarget(this);
}

/**
* @param {number} n
*/
Expand Down
Loading