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.
  • Loading branch information
deokjinkim committed Dec 29, 2022
1 parent 4830a6c commit cab08ea
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/events.js
Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/fs/watchers.js
Expand Up @@ -37,6 +37,8 @@ const {

const { toNamespacedPath } = require('path');

const { kEmptyObject } = require('internal/util');

const {
validateAbortSignal,
validateBoolean,
Expand Down Expand Up @@ -296,7 +298,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
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
4 changes: 3 additions & 1 deletion lib/internal/socketaddress.js
Expand Up @@ -11,6 +11,8 @@ const {
AF_INET6,
} = internalBinding('block_list');

const { kEmptyObject } = require('internal/util');

const {
validateObject,
validateString,
Expand Down Expand Up @@ -44,7 +46,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
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 @@ -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 };
Expand Down

0 comments on commit cab08ea

Please sign in to comment.