Skip to content

Commit 217cc13

Browse files
antimonyGuMylesBorins
authored andcommitted
events: improve performance caused by primordials
PR-URL: #30577 Refs: nodejs/code-and-learn#97 Refs: #29766 Refs: #29633 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent cc84dbf commit 217cc13

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

lib/events.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@
2121

2222
'use strict';
2323

24-
const { Math, Object, Reflect } = primordials;
25-
const apply = Reflect.apply;
24+
const {
25+
Math: {
26+
min: MathMin
27+
},
28+
Object: {
29+
defineProperty: ObjectDefineProperty,
30+
getPrototypeOf: ObjectGetPrototypeOf,
31+
create: ObjectCreate,
32+
keys: ObjectKeys,
33+
},
34+
Reflect: {
35+
apply: ReflectApply,
36+
ownKeys: ReflectOwnKeys,
37+
}
38+
} = primordials;
2639

2740
var spliceOne;
2841

@@ -65,7 +78,7 @@ function checkListener(listener) {
6578
}
6679
}
6780

68-
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
81+
ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
6982
enumerable: true,
7083
get: function() {
7184
return defaultMaxListeners;
@@ -83,8 +96,8 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
8396
EventEmitter.init = function() {
8497

8598
if (this._events === undefined ||
86-
this._events === Object.getPrototypeOf(this)._events) {
87-
this._events = Object.create(null);
99+
this._events === ObjectGetPrototypeOf(this)._events) {
100+
this._events = ObjectCreate(null);
88101
this._eventsCount = 0;
89102
}
90103

@@ -121,7 +134,7 @@ function identicalSequenceRange(a, b) {
121134
const rest = b.length - pos;
122135
if (rest > 3) {
123136
let len = 1;
124-
const maxLen = Math.min(a.length - i, rest);
137+
const maxLen = MathMin(a.length - i, rest);
125138
// Count the number of consecutive entries.
126139
while (maxLen > len && a[i + len] === b[pos + len]) {
127140
len++;
@@ -176,7 +189,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
176189
const capture = {};
177190
// eslint-disable-next-line no-restricted-syntax
178191
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
179-
Object.defineProperty(er, kEnhanceStackBeforeInspector, {
192+
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
180193
value: enhanceStackTrace.bind(this, er, capture),
181194
configurable: true
182195
});
@@ -207,12 +220,12 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
207220
return false;
208221

209222
if (typeof handler === 'function') {
210-
apply(handler, this, args);
223+
ReflectApply(handler, this, args);
211224
} else {
212225
const len = handler.length;
213226
const listeners = arrayClone(handler, len);
214227
for (var i = 0; i < len; ++i)
215-
apply(listeners[i], this, args);
228+
ReflectApply(listeners[i], this, args);
216229
}
217230

218231
return true;
@@ -227,7 +240,7 @@ function _addListener(target, type, listener, prepend) {
227240

228241
events = target._events;
229242
if (events === undefined) {
230-
events = target._events = Object.create(null);
243+
events = target._events = ObjectCreate(null);
231244
target._eventsCount = 0;
232245
} else {
233246
// To avoid recursion in the case that type === "newListener"! Before
@@ -341,7 +354,7 @@ EventEmitter.prototype.removeListener =
341354

342355
if (list === listener || list.listener === listener) {
343356
if (--this._eventsCount === 0)
344-
this._events = Object.create(null);
357+
this._events = ObjectCreate(null);
345358
else {
346359
delete events[type];
347360
if (events.removeListener)
@@ -390,11 +403,11 @@ EventEmitter.prototype.removeAllListeners =
390403
// Not listening for removeListener, no need to emit
391404
if (events.removeListener === undefined) {
392405
if (arguments.length === 0) {
393-
this._events = Object.create(null);
406+
this._events = ObjectCreate(null);
394407
this._eventsCount = 0;
395408
} else if (events[type] !== undefined) {
396409
if (--this._eventsCount === 0)
397-
this._events = Object.create(null);
410+
this._events = ObjectCreate(null);
398411
else
399412
delete events[type];
400413
}
@@ -403,12 +416,12 @@ EventEmitter.prototype.removeAllListeners =
403416

404417
// Emit removeListener for all listeners on all events
405418
if (arguments.length === 0) {
406-
for (const key of Object.keys(events)) {
419+
for (const key of ObjectKeys(events)) {
407420
if (key === 'removeListener') continue;
408421
this.removeAllListeners(key);
409422
}
410423
this.removeAllListeners('removeListener');
411-
this._events = Object.create(null);
424+
this._events = ObjectCreate(null);
412425
this._eventsCount = 0;
413426
return this;
414427
}
@@ -478,7 +491,7 @@ function listenerCount(type) {
478491
}
479492

480493
EventEmitter.prototype.eventNames = function eventNames() {
481-
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
494+
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
482495
};
483496

484497
function arrayClone(arr, n) {

0 commit comments

Comments
 (0)