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:
Anatoli Papirovski 2018-05-01 13:32:10 +02:00
parent 2553377d11
commit fe8794560a
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 15 additions and 17 deletions

View File

@ -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();

View File

@ -235,9 +235,8 @@ function _addListener(target, type, listener, prepend) {
}
// Check for listener leak
if (!existing.warned) {
m = $getMaxListeners(target);
if (m && m > 0 && existing.length > m) {
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
@ -252,7 +251,6 @@ function _addListener(target, type, listener, prepend) {
process.emitWarning(w);
}
}
}
return target;
}