From 5b40e2f596971d430abd5a26d9cc0c8e7c519780 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Wed, 21 Apr 2021 09:00:12 +0430 Subject: [PATCH] events: use nullish coalencing operator PR-URL: https://github.com/nodejs/node/pull/38328 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- benchmark/events/ee-add-remove.js | 18 ++++++++++++++---- lib/events.js | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/benchmark/events/ee-add-remove.js b/benchmark/events/ee-add-remove.js index 715eab29a59c4c..8769125ced6f5f 100644 --- a/benchmark/events/ee-add-remove.js +++ b/benchmark/events/ee-add-remove.js @@ -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'; diff --git a/lib/events.js b/lib/events.js index cc29b3358e0ce2..7d219606a28b1b 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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