events: optimize condition for optimal scenario
Instead of always checking whether we've already warned about a possible EventEmitter memory leak, first run the rest of the code as accessing random properties on an Array is expensive. In addition, remove an unnecessary truthy check. PR-URL: https://github.com/nodejs/node/pull/20452 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
2553377d11
commit
fe8794560a
@ -2,7 +2,7 @@
|
||||
const common = require('../common.js');
|
||||
const events = require('events');
|
||||
|
||||
const bench = common.createBenchmark(main, { n: [25e4] });
|
||||
const bench = common.createBenchmark(main, { n: [1e6] });
|
||||
|
||||
function main({ n }) {
|
||||
const ee = new events.EventEmitter();
|
||||
|
@ -235,22 +235,20 @@ function _addListener(target, type, listener, prepend) {
|
||||
}
|
||||
|
||||
// Check for listener leak
|
||||
if (!existing.warned) {
|
||||
m = $getMaxListeners(target);
|
||||
if (m && m > 0 && existing.length > m) {
|
||||
existing.warned = true;
|
||||
// No error code for this since it is a Warning
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
const w = new Error('Possible EventEmitter memory leak detected. ' +
|
||||
`${existing.length} ${String(type)} listeners ` +
|
||||
'added. Use emitter.setMaxListeners() to ' +
|
||||
'increase limit');
|
||||
w.name = 'MaxListenersExceededWarning';
|
||||
w.emitter = target;
|
||||
w.type = type;
|
||||
w.count = existing.length;
|
||||
process.emitWarning(w);
|
||||
}
|
||||
m = $getMaxListeners(target);
|
||||
if (m > 0 && existing.length > m && !existing.warned) {
|
||||
existing.warned = true;
|
||||
// No error code for this since it is a Warning
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
const w = new Error('Possible EventEmitter memory leak detected. ' +
|
||||
`${existing.length} ${String(type)} listeners ` +
|
||||
'added. Use emitter.setMaxListeners() to ' +
|
||||
'increase limit');
|
||||
w.name = 'MaxListenersExceededWarning';
|
||||
w.emitter = target;
|
||||
w.type = type;
|
||||
w.count = existing.length;
|
||||
process.emitWarning(w);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user