Skip to content

http: keep request option copies in fast mode - #67395

Open
gurgunday wants to merge 1 commit into
nodejs:mainfrom
gurgunday:perf/http-client-options-copy
Open

gurgunday wants to merge 1 commit into
nodejs:mainfrom
gurgunday:perf/http-client-options-copy

Conversation

@gurgunday

@gurgunday gurgunday commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Replaces the { __proto__: null, ...options } copies on the client request path and the http/https Agent constructors with { ...options, __proto__: null } so they stay out of dictionary mode

benchmark/http/create-clientrequest.js (50 runs at the default e=1, 30 runs with --set e=6):

arg url e=1 e=6
URL long +17.75 % *** +59.93 % ***
string long +15.64 % *** +55.37 % ***
options long +16.06 % *** +41.99 % ***
URL idn +20.46 % *** +67.04 % ***
string idn +8.78 % *** +52.59 % ***
options idn +13.40 % *** +41.10 % ***
URL wpt +17.28 % *** +56.80 % ***
string wpt +13.70 % *** +54.19 % ***
options wpt +2.76 % +33.58 % ***

Server benchmarks driven by benchmarker=test-double-http(s), where the
client half is http.request (30 runs, c=50 chunks=1 len=4 type=buffer):

http/simple.js +6.28 % **
https/simple.js +11.20 % ***

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. https Issues and PRs related to the https subsystem. needs-ci PRs that need a full CI run. labels Nov 10, 2026
@gurgunday gurgunday added the performance Issues and PRs related to the performance of Node.js. label Nov 10, 2026
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.67%. Comparing base (55da353) to head (98f32da).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67395   +/-   ##
=======================================
  Coverage   90.66%   90.67%           
=======================================
  Files         799      799           
  Lines      280245   280243    -2     
  Branches    54121    54121           
=======================================
+ Hits       254097   254098    +1     
- Misses      16277    16278    +1     
+ Partials     9871     9867    -4     
Files with missing lines Coverage Δ
lib/https.js 96.89% <100.00%> (ø)
lib/internal/http/agent.js 96.64% <100.00%> (ø)
lib/internal/http/client.js 97.15% <100.00%> (ø)

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pimterry pimterry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat, it's a good spot of a case we can speed up a bit.

That said, I pointed the new Fable at it to test this and explore a bit, and I think there's an even faster option 😄: { ...options, __proto__: null } (i.e. switching the order so we spread first, and __proto__ comes last).

This creates the object with the fast path clone for spread (here) but directly sets the prototype up front, so it skips the subsequent method call and prototype change of ObjectSetPrototypeOf(...), and still ends up in fast mode.

Do you want to take a look? Or if not I agree this is good regardless, we could merge now and test further improvements later.

@gurgunday

Copy link
Copy Markdown
Member Author

Wow, that looks great, yeah let's do that instead. I'll test and push the change

Comment thread lib/internal/http/agent.js Outdated
EventEmitter.call(this);

this.options = { __proto__: null, ...options };
this.options = ObjectSetPrototypeOf({ ...options }, null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ObjectSetPrototypeOf call also has overhead. We can achieve the same fast-properties result with something like...

const kNullProtoObject = { __proto__: null };
this.options = { __proto__: kNullProtoObject, ...options };

Should come out a bit faster.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey James, thanks for the review

what do you think about this?

it's 3-4% slower than your suggestion, but it still doesn't use ObjectSetPrototypeOf while keeping fast properties and the same behavior in all cases- i'm kind of surprised it works to be honest 😂

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>
@gurgunday
gurgunday force-pushed the perf/http-client-options-copy branch from 24cb5f2 to 98f32da Compare November 15, 2026 19:21
@gurgunday
gurgunday requested a review from pimterry November 15, 2026 19:43

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@gurgunday gurgunday added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. author ready PRs with CI started, the required approvals, and no outstanding review comments. and removed author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Nov 15, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 15, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

EventEmitter.call(this);

this.options = { __proto__: null, ...options };
this.options = { ...options, __proto__: null };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The { __proto__: kNullProtoObject, ...options } approach is better, I believe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues and PRs related to the http subsystem. https Issues and PRs related to the https subsystem. needs-ci PRs that need a full CI run. performance Issues and PRs related to the performance of Node.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants