From 7cbbd8fd158f606f9d85addbfb272d9c75ca511f Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 13 Jun 2020 13:35:16 -0400 Subject: [PATCH] events: improve listeners() performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/33863 Reviewed-By: Robert Nagy Reviewed-By: Anna Henningsen Reviewed-By: Gerhard Stöbich Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Yongsheng Zhang --- benchmark/events/ee-listeners.js | 2 +- lib/events.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/benchmark/events/ee-listeners.js b/benchmark/events/ee-listeners.js index 08631e95e2165a..9de8d04a175b4b 100644 --- a/benchmark/events/ee-listeners.js +++ b/benchmark/events/ee-listeners.js @@ -14,7 +14,7 @@ function main({ n, listeners, raw }) { for (let k = 0; k < listeners; k += 1) { ee.on('dummy0', () => {}); - ee.on('dummy1', () => {}); + ee.once('dummy1', () => {}); } if (raw === 'true') { diff --git a/lib/events.js b/lib/events.js index 06fed54c61bbdc..51259e6821bf23 100644 --- a/lib/events.js +++ b/lib/events.js @@ -22,7 +22,6 @@ 'use strict'; const { - Array, Boolean, Error, MathMin, @@ -613,9 +612,11 @@ function arrayClone(arr) { } function unwrapListeners(arr) { - const ret = new Array(arr.length); + const ret = arrayClone(arr); for (let i = 0; i < ret.length; ++i) { - ret[i] = arr[i].listener || arr[i]; + const orig = ret[i].listener; + if (typeof orig === 'function') + ret[i] = orig; } return ret; }