Skip to content

Commit 369b9c8

Browse files
authored
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. Use kEmptyObject, a frozen object with a null prototype and no properties, as the prototype of each copy instead of null. The copy still inherits nothing, has the same own properties in the same order, and stays in fast mode. 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 2 to 24 % at the default e=1 (50 runs) and by 49 to 87 % with e=6 (30 runs). Assisted-by: claude:opus-5.5 Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day> PR-URL: #67395 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 3e797cb commit 369b9c8

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 = { __proto__: kEmptyObject, ...options };
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
@@ -172,7 +172,7 @@ function Agent(options) {
172172

173173
EventEmitter.call(this);
174174

175-
this.options = { __proto__: null, ...options };
175+
this.options = { __proto__: kEmptyObject, ...options };
176176

177177
this.defaultPort = this.options.defaultPort || 80;
178178
this.protocol = this.options.protocol || 'http:';
@@ -395,7 +395,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
395395
}
396396

397397
// Here the agent options will override per-request options.
398-
options = { __proto__: null, ...options, ...this.options };
398+
options = { __proto__: kEmptyObject, ...options, ...this.options };
399399
if (options.socketPath)
400400
options.path = options.socketPath;
401401

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

473473
Agent.prototype.createSocket = function createSocket(req, options, cb) {
474474
// Here the agent options will override per-request options.
475-
options = { __proto__: null, ...options, ...this.options };
475+
options = { __proto__: kEmptyObject, ...options, ...this.options };
476476
if (options.socketPath)
477477
options.path = options.socketPath;
478478

‎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 = { __proto__: kEmptyObject, ...input, ...options };
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 = { __proto__: kEmptyObject, ...options };
417417

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

0 commit comments

Comments
 (0)