Skip to content

Commit

Permalink
lib: use kEmptyObject as default value for options
Browse files Browse the repository at this point in the history
`kEmptyObject` is more suitable than {} if options don't
need mutation.

PR-URL: #46011
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
deokjinkim authored and juanarbol committed Jan 31, 2023
1 parent 02a61dd commit c1cc1f9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
* @param {{ signal: AbortSignal; }} [options]
* @returns {AsyncIterator}
*/
function on(emitter, event, options) {
function on(emitter, event, options = kEmptyObject) {
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/socketaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {

const {
customInspectSymbol: kInspect,
kEmptyObject,
} = require('internal/util');

const { inspect } = require('internal/util/inspect');
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1583,7 +1584,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 };
Expand Down

0 comments on commit c1cc1f9

Please sign in to comment.