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 performance caused by primordials #30577

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
45 changes: 29 additions & 16 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@

'use strict';

const { Math, Object, Reflect } = primordials;
const apply = Reflect.apply;
const {
Math: {
min: MathMin
},
Object: {
defineProperty: ObjectDefineProperty,
getPrototypeOf: ObjectGetPrototypeOf,
create: ObjectCreate,
keys: ObjectKeys,
},
Reflect: {
apply: ReflectApply,
ownKeys: ReflectOwnKeys,
}
} = primordials;

var spliceOne;

Expand Down Expand Up @@ -65,7 +78,7 @@ function checkListener(listener) {
}
}

Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
enumerable: true,
get: function() {
return defaultMaxListeners;
Expand All @@ -83,8 +96,8 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
EventEmitter.init = function() {

if (this._events === undefined ||
this._events === Object.getPrototypeOf(this)._events) {
this._events = Object.create(null);
this._events === ObjectGetPrototypeOf(this)._events) {
this._events = ObjectCreate(null);
this._eventsCount = 0;
}

Expand Down Expand Up @@ -121,7 +134,7 @@ function identicalSequenceRange(a, b) {
const rest = b.length - pos;
if (rest > 3) {
let len = 1;
const maxLen = Math.min(a.length - i, rest);
const maxLen = MathMin(a.length - i, rest);
// Count the number of consecutive entries.
while (maxLen > len && a[i + len] === b[pos + len]) {
len++;
Expand Down Expand Up @@ -176,7 +189,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
const capture = {};
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
Object.defineProperty(er, kEnhanceStackBeforeInspector, {
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
value: enhanceStackTrace.bind(this, er, capture),
configurable: true
});
Expand Down Expand Up @@ -207,12 +220,12 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
return false;

if (typeof handler === 'function') {
apply(handler, this, args);
ReflectApply(handler, this, args);
} else {
const len = handler.length;
const listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
apply(listeners[i], this, args);
ReflectApply(listeners[i], this, args);
}

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

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

if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0)
this._events = Object.create(null);
this._events = ObjectCreate(null);
else {
delete events[type];
if (events.removeListener)
Expand Down Expand Up @@ -390,11 +403,11 @@ EventEmitter.prototype.removeAllListeners =
// Not listening for removeListener, no need to emit
if (events.removeListener === undefined) {
if (arguments.length === 0) {
this._events = Object.create(null);
this._events = ObjectCreate(null);
this._eventsCount = 0;
} else if (events[type] !== undefined) {
if (--this._eventsCount === 0)
this._events = Object.create(null);
this._events = ObjectCreate(null);
else
delete events[type];
}
Expand All @@ -403,12 +416,12 @@ EventEmitter.prototype.removeAllListeners =

// Emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (const key of Object.keys(events)) {
for (const key of ObjectKeys(events)) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = Object.create(null);
this._events = ObjectCreate(null);
this._eventsCount = 0;
return this;
}
Expand Down Expand Up @@ -478,7 +491,7 @@ function listenerCount(type) {
}

EventEmitter.prototype.eventNames = function eventNames() {
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
};

function arrayClone(arr, n) {
Expand Down