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 common = require('../common.js');
|
||||||
const events = require('events');
|
const events = require('events');
|
||||||
|
|
||||||
const bench = common.createBenchmark(main, { n: [25e4] });
|
const bench = common.createBenchmark(main, { n: [1e6] });
|
||||||
|
|
||||||
function main({ n }) {
|
function main({ n }) {
|
||||||
const ee = new events.EventEmitter();
|
const ee = new events.EventEmitter();
|
||||||
|
@ -235,9 +235,8 @@ function _addListener(target, type, listener, prepend) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for listener leak
|
// Check for listener leak
|
||||||
if (!existing.warned) {
|
|
||||||
m = $getMaxListeners(target);
|
m = $getMaxListeners(target);
|
||||||
if (m && m > 0 && existing.length > m) {
|
if (m > 0 && existing.length > m && !existing.warned) {
|
||||||
existing.warned = true;
|
existing.warned = true;
|
||||||
// No error code for this since it is a Warning
|
// No error code for this since it is a Warning
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
@ -252,7 +251,6 @@ function _addListener(target, type, listener, prepend) {
|
|||||||
process.emitWarning(w);
|
process.emitWarning(w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user