Skip to content

Avoid re-copying batched body data on every chunk - #220

Open
rcasazza wants to merge 1 commit into
httptoolkit:mainfrom
casazium:body-data-batching
Open

rcasazza wants to merge 1 commit into
httptoolkit:mainfrom
casazium:body-data-batching

Conversation

@rcasazza

Copy link
Copy Markdown

emitBodyDataEvents appends each incoming chunk to the pending batch with Buffer.concat, which copies the whole batch again for every chunk. With fast bodies a 20 ms batch grows to several MB from 64 KB chunks, so the cost is quadratic in batch size.

On localhost, a 200 MB passthrough download runs at ~1.5 GB/s with no listeners but ~340 MB/s as soon as anything listens for response-body-data. This collects the batch's chunks in an array and concatenates once per flush; with a listener the same download runs at ~1.6 GB/s.

200 MB passthrough, maxBodySize: 64 KB, 3 runs before after
no body-data listener 1431, 1482, 1882 MB/s 1400, 1489, 1861 MB/s
with a response-body-data listener 325, 355, 346 MB/s 1589, 1673, 1642 MB/s

Events, timestamps and isEnded are unchanged. Added a test that streams 2000 small chunks faster than the batch interval and checks the events reassemble to the exact body with one ended event. The Node test suite passes (846 passing, 2 pending); I haven't run the browser tests, but this code is server-side only.

🤖 Generated with Claude Code

emitBodyDataEvents concatenated each incoming chunk onto the pending batch,
copying the whole batch again every time. At high throughput a 20ms batch
grows to several MB from 64KB chunks, so this cost was quadratic: a 200MB
passthrough download dropped from ~1.5GB/s to ~340MB/s just from having a
body-data listener. Collect chunks in a list and concatenate once per flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Oct 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants