From 619d2775fd0cc424dcb4df7ed3be8dae08becd63 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 1/7] events: use nullish coalencing operator Could use this here instead of checking for all falsy values, a little bit more readable as well. --- lib/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ea4c8f755d2725b92799ca4ddd7347f6d876267e Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Wed, 21 Apr 2021 11:14:55 +0430 Subject: [PATCH 2/7] benchmark: create ee-newlistener.js Created the benchmark for new listeners. --- benchmark/events/ee-newlistener.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 benchmark/events/ee-newlistener.js diff --git a/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js new file mode 100644 index 00000000000000..5438d60e883fe2 --- /dev/null +++ b/benchmark/events/ee-newlistener.js @@ -0,0 +1,25 @@ +'use strict'; +const common = require('../common.js'); +const EventEmitter = require('events').EventEmitter; +const MathRandom = Math.random; + +const bench = common.createBenchmark(main, { n: [1e6] }); + +function main({ n }) { + const ee = new EventEmitter(); + const listeners = []; + + for (let i = 0; i < 10; i++) { + const listener = () => {}; + if (MathRandom() + .5) listener.listener = () => {}; + listeners.push(listener); + ee.on(`newListener${i}`, () => {}); + } + + bench.start(); + for (let i = 0; i < n; i++) { + for (let k = 0; k < listeners.length; k++) + ee.emit(`newListener${k}`, listeners[k].listener ?? listeners[k]); + } + bench.end(n); +} From 507ddec5b172cf3a31cc2c7bcbbb14349df747d6 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Wed, 21 Apr 2021 11:28:21 +0430 Subject: [PATCH 3/7] use increment assignment operator Used the increment assignment operator (`+=`) for consistency on all benchmarks. --- benchmark/events/ee-newlistener.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js index 5438d60e883fe2..d8d668482a3a4b 100644 --- a/benchmark/events/ee-newlistener.js +++ b/benchmark/events/ee-newlistener.js @@ -9,7 +9,7 @@ function main({ n }) { const ee = new EventEmitter(); const listeners = []; - for (let i = 0; i < 10; i++) { + for (let i = 0; i < 10; i += 1) { const listener = () => {}; if (MathRandom() + .5) listener.listener = () => {}; listeners.push(listener); @@ -17,8 +17,8 @@ function main({ n }) { } bench.start(); - for (let i = 0; i < n; i++) { - for (let k = 0; k < listeners.length; k++) + for (let i = 0; i < n; i += 1) { + for (let k = 0; k < listeners.length; k += 1) ee.emit(`newListener${k}`, listeners[k].listener ?? listeners[k]); } bench.end(n); From 5a11a92ab1b40566da5d9df7032eaeedc24abe2b Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Wed, 21 Apr 2021 20:17:43 +0430 Subject: [PATCH 4/7] Add a single event listener Added a single event listener instead of 10 and now only exercising the new changes, and fixed random listener property. --- benchmark/events/ee-newlistener.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js index d8d668482a3a4b..9532f9d50ed929 100644 --- a/benchmark/events/ee-newlistener.js +++ b/benchmark/events/ee-newlistener.js @@ -11,15 +11,16 @@ function main({ n }) { for (let i = 0; i < 10; i += 1) { const listener = () => {}; - if (MathRandom() + .5) listener.listener = () => {}; + if ((MathRandom() + .5) >= 1) listener.listener = () => {}; listeners.push(listener); - ee.on(`newListener${i}`, () => {}); } + ee.on('newListener', () => {}); + bench.start(); for (let i = 0; i < n; i += 1) { for (let k = 0; k < listeners.length; k += 1) - ee.emit(`newListener${k}`, listeners[k].listener ?? listeners[k]); + ee.emit('newListener', listeners[k].listener ?? listeners[k]); } bench.end(n); } From c32aa26e916d36c28fbf719c55575d9b7cdedc30 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Thu, 22 Apr 2021 20:33:07 +0430 Subject: [PATCH 5/7] Fix benchmark Fixed the `ee-newlistener` benchmark. --- benchmark/events/ee-newlistener.js | 33 +++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js index 9532f9d50ed929..57e40ba368fb5f 100644 --- a/benchmark/events/ee-newlistener.js +++ b/benchmark/events/ee-newlistener.js @@ -1,26 +1,35 @@ 'use strict'; const common = require('../common.js'); -const EventEmitter = require('events').EventEmitter; -const MathRandom = Math.random; +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 }) { +function main({ newListener, removeListener, n }) { const ee = new EventEmitter(); const listeners = []; - for (let i = 0; i < 10; i += 1) { - const listener = () => {}; - if ((MathRandom() + .5) >= 1) listener.listener = () => {}; - listeners.push(listener); - } + for (let k = 0; k < 10; k += 1) + listeners.push(() => {}); + + if (newListener === 1) + ee.on('newListener', (event, listener) => {}); - ee.on('newListener', () => {}); + if (removeListener === 1) + ee.on('removeListener', (event, listener) => {}); bench.start(); for (let i = 0; i < n; i += 1) { - for (let k = 0; k < listeners.length; k += 1) - ee.emit('newListener', listeners[k].listener ?? listeners[k]); + const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + for (let k = listeners.length; --k >= 0; /* empty */) { + ee.on(dummy, listeners[k]); + } + for (let k = listeners.length; --k >= 0; /* empty */) { + ee.removeListener(dummy, listeners[k]); + } } bench.end(n); } From 45408889ec363beec977b9f9420223af5946a9e7 Mon Sep 17 00:00:00 2001 From: Voltrex <62040526+VoltrexMaster@users.noreply.github.com> Date: Thu, 22 Apr 2021 22:09:26 +0430 Subject: [PATCH 6/7] CI check timeout CI check failed for timeout, pushing to fix. --- benchmark/events/ee-newlistener.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js index 57e40ba368fb5f..8769125ced6f5f 100644 --- a/benchmark/events/ee-newlistener.js +++ b/benchmark/events/ee-newlistener.js @@ -5,7 +5,7 @@ const { EventEmitter } = require('events'); const bench = common.createBenchmark(main, { newListener: [0, 1], removeListener: [0, 1], - n: [1e6] + n: [1e6], }); function main({ newListener, removeListener, n }) { From 7954246881eb8687dfdd24cd48d70fc936e2ac98 Mon Sep 17 00:00:00 2001 From: Mohammed Keyvanzadeh Date: Fri, 23 Apr 2021 01:23:34 +0430 Subject: [PATCH 7/7] Modify ee-add-remove benchmark Deleted the ee-newlistener benchmark and modified the ee-add-remove benchmark with the changes. --- benchmark/events/ee-add-remove.js | 18 +++++++++++---- benchmark/events/ee-newlistener.js | 35 ------------------------------ 2 files changed, 14 insertions(+), 39 deletions(-) delete mode 100644 benchmark/events/ee-newlistener.js 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/benchmark/events/ee-newlistener.js b/benchmark/events/ee-newlistener.js deleted file mode 100644 index 8769125ced6f5f..00000000000000 --- a/benchmark/events/ee-newlistener.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict'; -const common = require('../common.js'); -const { EventEmitter } = require('events'); - -const bench = common.createBenchmark(main, { - newListener: [0, 1], - removeListener: [0, 1], - n: [1e6], -}); - -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'; - for (let k = listeners.length; --k >= 0; /* empty */) { - ee.on(dummy, listeners[k]); - } - for (let k = listeners.length; --k >= 0; /* empty */) { - ee.removeListener(dummy, listeners[k]); - } - } - bench.end(n); -}