-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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: optimize for performance, remove _events use outside of events #17324
Changes from 2 commits
9cf8c68
9534a22
c3670cd
6e3b199
145bbc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [2e7], | ||
listeners: [1, 5, 10], | ||
}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
const listeners = Math.max(conf.listeners | 0, 1); | ||
|
||
const ee = new EventEmitter(); | ||
|
||
if (listeners === 1) | ||
n *= 5; | ||
else if (listeners === 5) | ||
n *= 2; | ||
|
||
for (var k = 0; k < listeners; k += 1) { | ||
ee.on('dummy', function() {}); | ||
ee.on(`dummy${k}`, function() {}); | ||
} | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
if (i % 3 === 0) | ||
ee.emit('dummy', true, 5); | ||
else if (i % 2 === 0) | ||
ee.emit('dummy', true, 5, 10, false); | ||
else | ||
ee.emit('dummy'); | ||
} | ||
bench.end(n); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
listeners: [1, 10], | ||
symbols: ['true', 'false'] | ||
}); | ||
|
||
function main(conf) { | ||
const n = conf.n | 0; | ||
const listeners = conf.listeners | 0; | ||
const useSymbols = conf.symbols === 'true'; | ||
|
||
const ee = new EventEmitter(); | ||
|
||
for (var k = 0; k < listeners; k += 1) { | ||
ee.on(`dummy0${k}`, function() {}); | ||
ee.on(`dummy1${k}`, function() {}); | ||
ee.on(`dummy2${k}`, function() {}); | ||
if (useSymbols) | ||
ee.on(Symbol(`dummy${k}`), function() {}); | ||
} | ||
|
||
ee.removeAllListeners('dummy01'); | ||
ee.removeAllListeners('dummy11'); | ||
ee.removeAllListeners('dummy21'); | ||
ee.removeAllListeners('dummy06'); | ||
ee.removeAllListeners('dummy16'); | ||
ee.removeAllListeners('dummy26'); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
ee.eventNames(); | ||
} | ||
bench.end(n); | ||
} |
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) | ||
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 kind of feel like something like (i.e. I’m not saying it is a better name, just that |
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
- `eventName` {any} | ||
|
||
Returns a copy of the array of listeners for the event named `eventName`, | ||
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'? 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. 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`. | ||
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 | ||
|
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 don't think automagically adjusting the
n
value is a good idea.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 mean, we don't have a ton of options here. The benchmark doesn't run long enough with 1 listener whereas setting a higher default
n
makes the other ones run way too long.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.
That's just a general benchmarking problem, just like any particular
n
value might cause things to run too fast or too slow on someone else's machine. At some point you will just need to supply your own set of values for each parameter if you have combinations that vary in speed that much.