Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: use nullish coalencing operator #38328

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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