Skip to content

Commit 28f9089

Browse files
deokjinkimRafaelGSS
authored andcommitted
lib: use kEmptyObject as default value for options
`kEmptyObject` is more suitable than {} if options don't need mutation. PR-URL: #46011 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 71bf513 commit 28f9089

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

lib/events.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
10021002
* @param {{ signal: AbortSignal; }} [options]
10031003
* @returns {AsyncIterator}
10041004
*/
1005-
function on(emitter, event, options) {
1006-
const signal = options?.signal;
1005+
function on(emitter, event, options = kEmptyObject) {
1006+
// Parameters validation
1007+
const signal = options.signal;
10071008
validateAbortSignal(signal, 'options.signal');
10081009
if (signal?.aborted)
10091010
throw new AbortError(undefined, { cause: signal?.reason });

lib/internal/fs/watchers.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const {
1414
ERR_INVALID_ARG_VALUE,
1515
},
1616
} = require('internal/errors');
17-
const { createDeferredPromise } = require('internal/util');
17+
const {
18+
createDeferredPromise,
19+
kEmptyObject,
20+
} = require('internal/util');
1821

1922
const {
2023
kFsStatsFieldsNumber,
@@ -296,7 +299,7 @@ ObjectDefineProperty(FSEvent.prototype, 'owner', {
296299
set(v) { return this[owner_symbol] = v; }
297300
});
298301

299-
async function* watch(filename, options = {}) {
302+
async function* watch(filename, options = kEmptyObject) {
300303
const path = toNamespacedPath(getValidatedPath(filename));
301304
validateObject(options, 'options');
302305

lib/internal/http2/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3326,7 +3326,7 @@ function createSecureServer(options, handler) {
33263326
function createServer(options, handler) {
33273327
if (typeof options === 'function') {
33283328
handler = options;
3329-
options = {};
3329+
options = kEmptyObject;
33303330
}
33313331
return new Http2Server(options, handler);
33323332
}

lib/internal/socketaddress.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626

2727
const {
2828
customInspectSymbol: kInspect,
29+
kEmptyObject,
2930
} = require('internal/util');
3031

3132
const { inspect } = require('internal/util/inspect');
@@ -44,7 +45,7 @@ class SocketAddress extends JSTransferable {
4445
return value?.[kHandle] !== undefined;
4546
}
4647

47-
constructor(options = {}) {
48+
constructor(options = kEmptyObject) {
4849
super();
4950
validateObject(options, 'options');
5051
let { family = 'ipv4' } = options;

lib/net.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const {
108108
} = require('internal/errors');
109109
const { isUint8Array } = require('internal/util/types');
110110
const { queueMicrotask } = require('internal/process/task_queues');
111+
const { kEmptyObject } = require('internal/util');
111112
const {
112113
validateAbortSignal,
113114
validateBoolean,
@@ -1584,7 +1585,7 @@ function Server(options, connectionListener) {
15841585

15851586
if (typeof options === 'function') {
15861587
connectionListener = options;
1587-
options = {};
1588+
options = kEmptyObject;
15881589
this.on('connection', connectionListener);
15891590
} else if (options == null || typeof options === 'object') {
15901591
options = { ...options };

0 commit comments

Comments
 (0)