From 369b9c88aced207e9f15d1ecb7075d710ae6a7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 18 Nov 2026 11:51:01 +0300 Subject: [PATCH] http: keep request option copies in fast mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 PR-URL: https://github.com/nodejs/node/pull/67395 Reviewed-By: Matteo Collina Reviewed-By: Tim Perry Reviewed-By: Ethan Arrowood --- lib/https.js | 2 +- lib/internal/http/agent.js | 6 +++--- lib/internal/http/client.js | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/https.js b/lib/https.js index 38623d258a2..efcbb83fa05 100644 --- a/lib/https.js +++ b/lib/https.js @@ -494,7 +494,7 @@ function Agent(options) { if (!(this instanceof Agent)) return new Agent(options); - options = { __proto__: null, ...options }; + options = { __proto__: kEmptyObject, ...options }; validateOpenSSLEngineOptions(options); options.defaultPort ??= 443; options.protocol ??= 'https:'; diff --git a/lib/internal/http/agent.js b/lib/internal/http/agent.js index e5686012ff2..013be05d1f9 100644 --- a/lib/internal/http/agent.js +++ b/lib/internal/http/agent.js @@ -172,7 +172,7 @@ function Agent(options) { EventEmitter.call(this); - this.options = { __proto__: null, ...options }; + this.options = { __proto__: kEmptyObject, ...options }; this.defaultPort = this.options.defaultPort || 80; this.protocol = this.options.protocol || 'http:'; @@ -395,7 +395,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, } // Here the agent options will override per-request options. - options = { __proto__: null, ...options, ...this.options }; + options = { __proto__: kEmptyObject, ...options, ...this.options }; if (options.socketPath) options.path = options.socketPath; @@ -472,7 +472,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, Agent.prototype.createSocket = function createSocket(req, options, cb) { // Here the agent options will override per-request options. - options = { __proto__: null, ...options, ...this.options }; + options = { __proto__: kEmptyObject, ...options, ...this.options }; if (options.socketPath) options.path = options.socketPath; diff --git a/lib/internal/http/client.js b/lib/internal/http/client.js index 880e0ee5240..4e7479d3377 100644 --- a/lib/internal/http/client.js +++ b/lib/internal/http/client.js @@ -26,7 +26,6 @@ const { Boolean, Error, NumberIsFinite, - ObjectAssign, ObjectDefineProperty, ObjectHasOwn, ObjectKeys, @@ -365,7 +364,8 @@ function ClientRequest(input, options, cb) { pathIsFromURL = false; } - options = ObjectAssign({ __proto__: null }, input, options); + // Avoid creating a dictionary-mode object. + options = { __proto__: kEmptyObject, ...input, ...options }; } // Propagate the user's highWaterMark to OutgoingMessage so that @@ -413,7 +413,7 @@ function ClientRequest(input, options, cb) { const defaultPort = options.defaultPort || (this.agent?.defaultPort); - const optsWithoutSignal = { __proto__: null, ...options }; + const optsWithoutSignal = { __proto__: kEmptyObject, ...options }; const port = optsWithoutSignal.port = options.port || defaultPort || 80; const host = optsWithoutSignal.host = validateHost(options.hostname, 'hostname') ||