Skip to content

Commit

Permalink
events: use nullish coalencing operator
Browse files Browse the repository at this point in the history
PR-URL: #38328
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
VoltrexKeyva authored and targos committed May 18, 2021
1 parent 8590c15 commit 5b40e2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions benchmark/events/ee-add-remove.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
'use strict';
const common = require('../common.js');
const events = require('events');
const { EventEmitter } = require('events');

const bench = common.createBenchmark(main, { n: [1e6] });
const bench = common.createBenchmark(main, {
newListener: [0, 1],
removeListener: [0, 1],
n: [1e6],
});

function main({ n }) {
const ee = new events.EventEmitter();
function main({ newListener, removeListener, n }) {
const ee = new EventEmitter();
const listeners = [];

for (let k = 0; k < 10; k += 1)
listeners.push(() => {});

if (newListener === 1)
ee.on('newListener', (event, listener) => {});

if (removeListener === 1)
ee.on('removeListener', (event, listener) => {});

bench.start();
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
Expand Down
2 changes: 1 addition & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function _addListener(target, type, listener, prepend) {
// adding it to the listeners, first emit "newListener".
if (events.newListener !== undefined) {
target.emit('newListener', type,
listener.listener ? listener.listener : listener);
listener.listener ?? listener);

// Re-assign `events` because a newListener handler could have caused the
// this._events to be assigned to a new object
Expand Down

0 comments on commit 5b40e2f

Please sign in to comment.