Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58a65d6

Browse filesBrowse files
apapirovskiMylesBorins
authored andcommitted
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: #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>
1 parent 74685f1 commit 58a65d6
Copy full SHA for 58a65d6

File tree

Expand file treeCollapse file tree

2 files changed

+15
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+15
-17
lines changed
Open diff view settings
Collapse file

‎benchmark/events/ee-add-remove.js‎

Copy file name to clipboardExpand all lines: benchmark/events/ee-add-remove.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33
const events = require('events');
44

5-
const bench = common.createBenchmark(main, { n: [25e4] });
5+
const bench = common.createBenchmark(main, { n: [1e6] });
66

77
function main({ n }) {
88
const ee = new events.EventEmitter();
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+14-16Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,20 @@ function _addListener(target, type, listener, prepend) {
235235
}
236236

237237
// Check for listener leak
238-
if (!existing.warned) {
239-
m = $getMaxListeners(target);
240-
if (m && m > 0 && existing.length > m) {
241-
existing.warned = true;
242-
// No error code for this since it is a Warning
243-
// eslint-disable-next-line no-restricted-syntax
244-
const w = new Error('Possible EventEmitter memory leak detected. ' +
245-
`${existing.length} ${String(type)} listeners ` +
246-
'added. Use emitter.setMaxListeners() to ' +
247-
'increase limit');
248-
w.name = 'MaxListenersExceededWarning';
249-
w.emitter = target;
250-
w.type = type;
251-
w.count = existing.length;
252-
process.emitWarning(w);
253-
}
238+
m = $getMaxListeners(target);
239+
if (m > 0 && existing.length > m && !existing.warned) {
240+
existing.warned = true;
241+
// No error code for this since it is a Warning
242+
// eslint-disable-next-line no-restricted-syntax
243+
const w = new Error('Possible EventEmitter memory leak detected. ' +
244+
`${existing.length} ${String(type)} listeners ` +
245+
'added. Use emitter.setMaxListeners() to ' +
246+
'increase limit');
247+
w.name = 'MaxListenersExceededWarning';
248+
w.emitter = target;
249+
w.type = type;
250+
w.count = existing.length;
251+
process.emitWarning(w);
254252
}
255253
}
256254

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.