-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
events: optimize for performance, remove _events use outside of events #17324
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
Changes from 1 commit
9cf8c68
9534a22
c3670cd
6e3b199
145bbc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Refactor lib & src code to eliminate all deep reaches into the internal _events dictionary object, instead use available APIs and add an extra method to EventEmitter: wrappedListeners.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -574,6 +574,15 @@ to indicate an unlimited number of listeners. | |
|
|
||
| Returns a reference to the `EventEmitter`, so that calls can be chained. | ||
|
|
||
| ### emitter.wrappedListeners(eventName) | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| - `eventName` {any} | ||
|
|
||
| Returns a copy of the array of listeners for the event named `eventName`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think saying 'event named' sounds a little confusing. Maybe something like 'event in' instead? Or maybe just 'event'?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change this but we have another 9 instances of this exact phrasing in this doc.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the current wording is fine here and if we do change it, I'd rather do that throughout the doc and in a separate PR for consistency |
||
| including any wrappers (such as those created by `.once`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing end parenthesis. Also, maybe link |
||
|
|
||
| [`--trace-warnings`]: cli.html#cli_trace_warnings | ||
| [`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners | ||
| [`domain`]: domain.html | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -400,8 +400,8 @@ EventEmitter.prototype.removeAllListeners = | |
| return this; | ||
| }; | ||
|
|
||
| EventEmitter.prototype.listeners = function listeners(type) { | ||
| const events = this._events; | ||
| function _listeners(target, type, unwrap) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would make sense to turn this into a closure where function makeListeners(unwrap) {
return function listeners(type) {
// Code of _listeners with `this` as `target`
}
}
EventEmitter.prototype.listeners = makeListeners(true);
EventEmitter.prototype.rawListeners = makeListeners(false);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just to mimic how we do it elsewhere in this file, namely No clue what's preferable. Maybe just stick with consistency for now?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no perf difference, then yes, just stick with this :) |
||
| const events = target._events; | ||
|
|
||
| if (events === undefined) | ||
| return []; | ||
|
|
@@ -411,9 +411,17 @@ EventEmitter.prototype.listeners = function listeners(type) { | |
| return []; | ||
|
|
||
| if (typeof evlistener === 'function') | ||
| return [evlistener.listener || evlistener]; | ||
| return unwrap ? [evlistener.listener || evlistener] : [evlistener]; | ||
|
|
||
| return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener); | ||
| } | ||
|
|
||
| EventEmitter.prototype.listeners = function listeners(type) { | ||
| return _listeners(this, type, true); | ||
| }; | ||
|
|
||
| return unwrapListeners(evlistener); | ||
| EventEmitter.prototype.wrappedListeners = function wrappedListeners(type) { | ||
| return _listeners(this, type, false); | ||
| }; | ||
|
|
||
| EventEmitter.listenerCount = function(emitter, type) { | ||
|
|
@@ -471,6 +479,13 @@ EventEmitter.prototype.eventNames = function eventNames() { | |
| return actualEventNames; | ||
| }; | ||
|
|
||
| function arrayClone(arr) { | ||
| const copy = new Array(arr.length); | ||
| for (var i = 0; i < arr.length; ++i) | ||
| copy[i] = arr[i]; | ||
| return copy; | ||
| } | ||
|
|
||
| function arrayCloneWithElement(arr, element, prepend) { | ||
| const len = arr.length; | ||
| const copy = new Array(len + 1); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of feel like something like
rawListeners()might be a better name, but this should be okay.(i.e. I’m not saying it is a better name, just that
wrappedListeners()does not immediately tell you what this does … neither doesrawListeners(), obviously, but I think it comes closer? 😄 )