From 0ee6cae60ace291ca18079de598f9ced96d9160f Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Thu, 29 Dec 2022 16:17:27 +0900 Subject: [PATCH] lib: use `kEmptyObject` as default value for options `kEmptyObject` is more suitable than {} if options don't need mutation. --- lib/events.js | 2 +- lib/internal/fs/watchers.js | 7 +++++-- lib/internal/http2/core.js | 2 +- lib/internal/socketaddress.js | 3 ++- lib/net.js | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/events.js b/lib/events.js index e05a3bc6b8b168..efce9b9c228c97 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1013,7 +1013,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) { * }} [options] * @returns {AsyncIterator} */ -function on(emitter, event, options = {}) { +function on(emitter, event, options = kEmptyObject) { // Parameters validation const signal = options.signal; validateAbortSignal(signal, 'options.signal'); 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 };