Skip to content

http: improve performance for end() with known-length string - #65466

Open
pimterry wants to merge 1 commit into
nodejs:mainfrom
pimterry:h1-clean-end-perf
Open

http: improve performance for end() with known-length string#65466
pimterry wants to merge 1 commit into
nodejs:mainfrom
pimterry:h1-clean-end-perf

Conversation

@pimterry

Copy link
Copy Markdown
Member

When you call end(data) on an outgoing HTTP request, we previously wrote the data, and then ran _send('', ...) just to trigger flushing the headers, which resulted in an extra zero-byte write call and bonus nextTick before 'finish'.

When the length is known and content-length is used (not chunking, so we don't need a terminator) we can skip this completely: prep the content-length header explicitly, and combine the finish into the existing write step.

This drops a nextTick, and goes from 2 write calls to 1 for the very common case of response.end(data) with a string, and for buffer data goes from 3 writes to 2.

In practice, in the tiny benchmark here which I think represents a very common use case (small API responses) I see this boosting HTTP RPS by up to 9% for writes up to 1KB or so. For larger responses the response write time starts to outweigh this, but I still see 3% RPS boost at 16KB. Needs autocannon to hit the rates required, you can test yourself with:

node benchmark/compare.js --old ./node-main --new ./node-pr --runs 20 --set benchmarker=autocannon --filter end-string http

Added a new test confirming the specific details and edge cases this touches, and updated one existing test which specifically asserted that end(data) triggered two writes, including the empty one.

This boosts RPS performance for the common API case where you call
`res.end(data)` with the entire response by up to 9%.

Signed-off-by: Tim Perry <pimterry@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Aug 21, 2026

@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

@mcollina
mcollina requested a review from anonrig August 21, 2026 19:24
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.13%. Comparing base (15940ef) to head (659d6b9).
⚠️ Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65466      +/-   ##
==========================================
- Coverage   90.14%   90.13%   -0.01%     
==========================================
  Files         751      751              
  Lines      252298   252333      +35     
  Branches    47446    47449       +3     
==========================================
+ Hits       227436   227451      +15     
- Misses      16175    16186      +11     
- Partials     8687     8696       +9     
Files with missing lines Coverage Δ
lib/_http_outgoing.js 97.76% <100.00%> (-0.02%) ⬇️

... and 34 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 added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 21, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 22, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@bjohansebas bjohansebas added the needs-benchmark-ci PR that need a benchmark CI run. label Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http Issues or PRs related to the http subsystem. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants