Skip to content

Commit 4b7a530

Browse files
committed
lib: switch to object spread where possible
Use the object spread notation instead of using Object.assign. It is not only easier to read it is also faster as of V8 6.8. PR-URL: #25104 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 6f8ccef commit 4b7a530

File tree

10 files changed

+32
-31
lines changed

10 files changed

+32
-31
lines changed

lib/.eslintrc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rules:
2+
prefer-object-spread: error
23
no-restricted-syntax:
34
# Config copied from .eslintrc.js
45
- error

lib/_tls_wrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ function initRead(tls, wrapped) {
291291
*/
292292

293293
function TLSSocket(socket, opts) {
294-
const tlsOptions = Object.assign({}, opts);
294+
const tlsOptions = { ...opts };
295295

296296
if (tlsOptions.ALPNProtocols)
297297
tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions);

lib/child_process.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function normalizeExecArgs(command, options, callback) {
136136
}
137137

138138
// Make a shallow copy so we don't clobber the user's options object.
139-
options = Object.assign({}, options);
139+
options = { ...options };
140140
options.shell = typeof options.shell === 'string' ? options.shell : true;
141141

142142
return {
@@ -470,7 +470,7 @@ function normalizeSpawnArguments(file, args, options) {
470470
}
471471

472472
// Make a shallow copy so we don't clobber the user's options object.
473-
options = Object.assign({}, options);
473+
options = { ...options };
474474

475475
if (options.shell) {
476476
const command = [file].concat(args).join(' ');

lib/internal/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ function setupChannel(target, channel) {
636636
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
637637
}
638638

639-
options = Object.assign({ swallowErrors: false }, options);
639+
options = { swallowErrors: false, ...options };
640640

641641
if (this.connected) {
642642
return this._send(message, handle, options, callback);

lib/internal/console/constructor.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,11 @@ Console.prototype.warn = function warn(...args) {
294294
Console.prototype.error = Console.prototype.warn;
295295

296296
Console.prototype.dir = function dir(object, options) {
297-
options = Object.assign({
298-
customInspect: false
299-
}, this[kGetInspectOptions](this._stdout), options);
300-
this[kWriteToConsole](kUseStdout, util.inspect(object, options));
297+
this[kWriteToConsole](kUseStdout, util.inspect(object, {
298+
customInspect: false,
299+
...this[kGetInspectOptions](this._stdout),
300+
...options
301+
}));
301302
};
302303

303304
Console.prototype.time = function time(label = 'default') {

lib/internal/http2/compat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ class Http2ServerResponse extends Stream {
512512
}
513513

514514
getHeaders() {
515-
return Object.assign({}, this[kHeaders]);
515+
return { ...this[kHeaders] };
516516
}
517517

518518
hasHeader(name) {

lib/internal/http2/core.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ function pingCallback(cb) {
790790
// 6. enablePush must be a boolean
791791
// All settings are optional and may be left undefined
792792
function validateSettings(settings) {
793-
settings = Object.assign({}, settings);
793+
settings = { ...settings };
794794
assertWithinRange('headerTableSize',
795795
settings.headerTableSize,
796796
0, kMaxInt);
@@ -1443,7 +1443,7 @@ class ClientHttp2Session extends Http2Session {
14431443
assertIsObject(options, 'options');
14441444

14451445
headers = Object.assign(Object.create(null), headers);
1446-
options = Object.assign({}, options);
1446+
options = { ...options };
14471447

14481448
if (headers[HTTP2_HEADER_METHOD] === undefined)
14491449
headers[HTTP2_HEADER_METHOD] = HTTP2_METHOD_GET;
@@ -1848,7 +1848,7 @@ class Http2Stream extends Duplex {
18481848
throw new ERR_HTTP2_INVALID_STREAM();
18491849

18501850
assertIsObject(options, 'options');
1851-
options = Object.assign({}, options);
1851+
options = { ...options };
18521852
validatePriorityOptions(options);
18531853

18541854
const priorityFn = submitPriority.bind(this, options);
@@ -2257,7 +2257,7 @@ class ServerHttp2Stream extends Http2Stream {
22572257
throw new ERR_INVALID_CALLBACK();
22582258

22592259
assertIsObject(options, 'options');
2260-
options = Object.assign({}, options);
2260+
options = { ...options };
22612261
options.endStream = !!options.endStream;
22622262

22632263
assertIsObject(headers, 'headers');
@@ -2322,7 +2322,7 @@ class ServerHttp2Stream extends Http2Stream {
23222322
const state = this[kState];
23232323

23242324
assertIsObject(options, 'options');
2325-
options = Object.assign({}, options);
2325+
options = { ...options };
23262326

23272327
const session = this[kSession];
23282328
debug(`Http2Stream ${this[kID]} [Http2Session ` +
@@ -2378,7 +2378,7 @@ class ServerHttp2Stream extends Http2Stream {
23782378
const session = this[kSession];
23792379

23802380
assertIsObject(options, 'options');
2381-
options = Object.assign({}, options);
2381+
options = { ...options };
23822382

23832383
if (options.offset !== undefined && typeof options.offset !== 'number')
23842384
throw new ERR_INVALID_OPT_VALUE('offset', options.offset);
@@ -2441,7 +2441,7 @@ class ServerHttp2Stream extends Http2Stream {
24412441
throw new ERR_HTTP2_HEADERS_SENT();
24422442

24432443
assertIsObject(options, 'options');
2444-
options = Object.assign({}, options);
2444+
options = { ...options };
24452445

24462446
if (options.offset !== undefined && typeof options.offset !== 'number')
24472447
throw new ERR_INVALID_OPT_VALUE('offset', options.offset);
@@ -2667,10 +2667,10 @@ function connectionListener(socket) {
26672667

26682668
function initializeOptions(options) {
26692669
assertIsObject(options, 'options');
2670-
options = Object.assign({}, options);
2670+
options = { ...options };
26712671
options.allowHalfOpen = true;
26722672
assertIsObject(options.settings, 'options.settings');
2673-
options.settings = Object.assign({}, options.settings);
2673+
options.settings = { ...options.settings };
26742674

26752675
// Used only with allowHTTP1
26762676
options.Http1IncomingMessage = options.Http1IncomingMessage ||
@@ -2775,7 +2775,7 @@ function connect(authority, options, listener) {
27752775
}
27762776

27772777
assertIsObject(options, 'options');
2778-
options = Object.assign({}, options);
2778+
options = { ...options };
27792779

27802780
if (typeof authority === 'string')
27812781
authority = new URL(authority);

lib/repl.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ function hasOwnProperty(obj, prop) {
129129
// and it can be overridden by custom print functions, such as `probe` or
130130
// `eyes.js`.
131131
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
132-
writer.options =
133-
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
132+
writer.options = { ...util.inspect.defaultOptions, showProxy: true };
134133

135134
exports._builtinLibs = builtinLibs;
136135

@@ -509,7 +508,7 @@ function REPLServer(prompt,
509508
if (self.useColors && self.writer === writer) {
510509
// Turn on ANSI coloring.
511510
self.writer = (obj) => util.inspect(obj, self.writer.options);
512-
self.writer.options = Object.assign({}, writer.options, { colors: true });
511+
self.writer.options = { ...writer.options, colors: true };
513512
}
514513

515514
function filterInternalStackFrames(structuredStack) {

lib/tls.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,13 @@ class SecurePair extends EventEmitter {
270270
this.credentials = secureContext;
271271

272272
this.encrypted = socket1;
273-
this.cleartext = new exports.TLSSocket(socket2, Object.assign({
274-
secureContext, isServer, requestCert, rejectUnauthorized
275-
}, options));
273+
this.cleartext = new exports.TLSSocket(socket2, {
274+
secureContext,
275+
isServer,
276+
requestCert,
277+
rejectUnauthorized,
278+
...options
279+
});
276280
this.cleartext.once('secure', () => this.emit('secure'));
277281
}
278282

lib/vm.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ function runInContext(code, contextifiedSandbox, options) {
290290
[kParsingContext]: contextifiedSandbox
291291
};
292292
} else {
293-
options = Object.assign({}, options, {
294-
[kParsingContext]: contextifiedSandbox
295-
});
293+
options = { ...options, [kParsingContext]: contextifiedSandbox };
296294
}
297295
return createScript(code, options)
298296
.runInContext(contextifiedSandbox, options);
@@ -303,9 +301,7 @@ function runInNewContext(code, sandbox, options) {
303301
options = { filename: options };
304302
}
305303
sandbox = createContext(sandbox, getContextOptions(options));
306-
options = Object.assign({}, options, {
307-
[kParsingContext]: sandbox
308-
});
304+
options = { ...options, [kParsingContext]: sandbox };
309305
return createScript(code, options).runInNewContext(sandbox, options);
310306
}
311307

0 commit comments

Comments
 (0)