From 28f9089b83bded08a90f820916073b60554c9165 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sat, 31 Dec 2022 16:33:39 +0900 Subject: [PATCH] lib: use `kEmptyObject` as default value for options `kEmptyObject` is more suitable than {} if options don't need mutation. PR-URL: https://github.com/nodejs/node/pull/46011 Reviewed-By: Antoine du Hamel Reviewed-By: Colin Ihrig Reviewed-By: Rafael Gonzaga Reviewed-By: Luigi Pinca --- lib/events.js | 5 +++-- lib/internal/fs/watchers.js | 7 +++++-- lib/internal/http2/core.js | 2 +- lib/internal/socketaddress.js | 3 ++- lib/net.js | 3 ++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/events.js b/lib/events.js index 004112cd353db8..4cad911c6bc8e6 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1002,8 +1002,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) { * @param {{ signal: AbortSignal; }} [options] * @returns {AsyncIterator} */ -function on(emitter, event, options) { - const signal = options?.signal; +function on(emitter, event, options = kEmptyObject) { + // Parameters validation + const signal = options.signal; validateAbortSignal(signal, 'options.signal'); if (signal?.aborted) throw new AbortError(undefined, { cause: signal?.reason }); diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index 7b820e70df1613..bc4555584ab1f9 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -14,7 +14,10 @@ const { ERR_INVALID_ARG_VALUE, }, } = require('internal/errors'); -const { createDeferredPromise } = require('internal/util'); +const { + createDeferredPromise, + kEmptyObject, +} = require('internal/util'); const { kFsStatsFieldsNumber, @@ -296,7 +299,7 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', { set(v) { return this[owner_symbol] = v; } }); -async function* watch(filename, options = {}) { +async function* watch(filename, options = kEmptyObject) { const path = toNamespacedPath(getValidatedPath(filename)); validateObject(options, 'options'); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 000bc1ba85d7e4..6ddc1f609cf2fb 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -3326,7 +3326,7 @@ function createSecureServer(options, handler) { function createServer(options, handler) { if (typeof options === 'function') { handler = options; - options = {}; + options = kEmptyObject; } return new Http2Server(options, handler); } diff --git a/lib/internal/socketaddress.js b/lib/internal/socketaddress.js index 506f397180e6a1..96554f5f28d874 100644 --- a/lib/internal/socketaddress.js +++ b/lib/internal/socketaddress.js @@ -26,6 +26,7 @@ const { const { customInspectSymbol: kInspect, + kEmptyObject, } = require('internal/util'); const { inspect } = require('internal/util/inspect'); @@ -44,7 +45,7 @@ class SocketAddress extends JSTransferable { return value?.[kHandle] !== undefined; } - constructor(options = {}) { + constructor(options = kEmptyObject) { super(); validateObject(options, 'options'); let { family = 'ipv4' } = options; diff --git a/lib/net.js b/lib/net.js index fd76c565b1f2aa..fa0a56e482b712 100644 --- a/lib/net.js +++ b/lib/net.js @@ -108,6 +108,7 @@ const { } = require('internal/errors'); const { isUint8Array } = require('internal/util/types'); const { queueMicrotask } = require('internal/process/task_queues'); +const { kEmptyObject } = require('internal/util'); const { validateAbortSignal, validateBoolean, @@ -1584,7 +1585,7 @@ function Server(options, connectionListener) { if (typeof options === 'function') { connectionListener = options; - options = {}; + options = kEmptyObject; this.on('connection', connectionListener); } else if (options == null || typeof options === 'object') { options = { ...options };