Skip to content

Commit 98f32da

Browse files
committed
http: keep request option copies in fast mode
The options of every client request are copied several times: once in ClientRequest to merge the URL and the options, once more to build the options handed to the agent, and again in Agent#addRequest (on every request, including keep-alive reuse) and createSocket to merge in the agent options. Each copy was a `{ __proto__: null, ...}` literal or an Object.assign() onto `{ __proto__: null }`. V8 creates such an object in dictionary mode, and copying from a dictionary-mode source takes the generic slow path, so every copy in the chain was slow and fed a slow source into the next one. Put the spread first and `__proto__: null` last in each literal. V8 then clones the source straight into an object with a null prototype, which leaves a fast-mode object with the same own properties in the same order. The same is done for the copy of the options in the http and https Agent constructors, which is the source of the agent options merged into every request. benchmark/http/create-clientrequest.js improves by 5 to 24 % at the default e=1 (100 runs) and by 47 to 82 % with e=6 (60 runs). Assisted-by: claude:opus-5.5 Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 55da353 commit 98f32da

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

‎lib/https.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ function Agent(options) {
494494
if (!(this instanceof Agent))
495495
return new Agent(options);
496496

497-
options = { __proto__: null, ...options };
497+
options = { ...options, __proto__: null };
498498
validateOpenSSLEngineOptions(options);
499499
options.defaultPort ??= 443;
500500
options.protocol ??= 'https:';

‎lib/internal/http/agent.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function Agent(options) {
158158

159159
EventEmitter.call(this);
160160

161-
this.options = { __proto__: null, ...options };
161+
this.options = { ...options, __proto__: null };
162162

163163
this.defaultPort = this.options.defaultPort || 80;
164164
this.protocol = this.options.protocol || 'http:';
@@ -375,7 +375,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
375375
}
376376

377377
// Here the agent options will override per-request options.
378-
options = { __proto__: null, ...options, ...this.options };
378+
options = { ...options, ...this.options, __proto__: null };
379379
if (options.socketPath)
380380
options.path = options.socketPath;
381381

@@ -445,7 +445,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
445445

446446
Agent.prototype.createSocket = function createSocket(req, options, cb) {
447447
// Here the agent options will override per-request options.
448-
options = { __proto__: null, ...options, ...this.options };
448+
options = { ...options, ...this.options, __proto__: null };
449449
if (options.socketPath)
450450
options.path = options.socketPath;
451451

‎lib/internal/http/client.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const {
2626
Boolean,
2727
Error,
2828
NumberIsFinite,
29-
ObjectAssign,
3029
ObjectDefineProperty,
3130
ObjectHasOwn,
3231
ObjectKeys,
@@ -365,7 +364,8 @@ function ClientRequest(input, options, cb) {
365364
pathIsFromURL = false;
366365
}
367366

368-
options = ObjectAssign({ __proto__: null }, input, options);
367+
// Avoid creating a dictionary-mode object.
368+
options = { ...input, ...options, __proto__: null };
369369
}
370370

371371
// Propagate the user's highWaterMark to OutgoingMessage so that
@@ -413,7 +413,7 @@ function ClientRequest(input, options, cb) {
413413
const defaultPort = options.defaultPort ||
414414
(this.agent?.defaultPort);
415415

416-
const optsWithoutSignal = { __proto__: null, ...options };
416+
const optsWithoutSignal = { ...options, __proto__: null };
417417

418418
const port = optsWithoutSignal.port = options.port || defaultPort || 80;
419419
const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||

0 commit comments

Comments
 (0)