Repository navigation
perf(node): read buffered response bodies natively instead of chunk by chunk - #646
Merged
Merged
Conversation
…y chunk `arrayBuffer()`, `bytes()`, `text()` and `json()` now collect the body on the native side and hand it to JavaScript once. Before, they read the body as a stream, and a gzip body arrived as many ~4 KB pieces, each crossing into JS. `response.body` and `clone()` still stream as before. - the abort channel is created with the response, so an abort also stops a native read (and one that arrives before the read starts) - reading a consumed body throws `TypeError: Body is unusable` instead of panicking - `json()` decodes the bytes as UTF-8 (stripping a BOM), as Fetch does Closes apify#641 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… throwing Matches Fetch, where `body` stays a locked stream after `text()` and further reads throw `Body is unusable`.
barjin
approved these changes
Dec 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
arrayBuffer(),bytes(),text()andjson()read the body in Rust and pass it to JS once. Before, every decompressed ~4 KB piece crossed into JS separately: 74 pieces for a 300 KB gzip body.response.bodyandclone()still stream as before.AbortSignalalso stops a native read, including when it fires before the read starts.TypeError: Body is unusable: Body has already been readinstead of reaching apanic!.json()decodes as UTF-8 and strips a BOM, as Fetch does.Evidence
The numbers below come from the
repro.mjsbenchmark script in #641, run unchanged except that it imports the local build instead ofimpitfrom npm. Bothmasterand this branch were release builds, run on a local Windows 11 development machine with Node 26, 3 runs each.Before (
master). JS-thread ms per response:After (this branch):
Node's own
fetchon the same gzip body: 0.62–0.83.New tests:
The full Node suite passes locally except
Basic requests > to an http(s):// domain. That test fetches the liveapify.comsite, which refused connections from my local network. It fails the same way onmaster, so it is unrelated; CI should run it normally.Memory: a leak test ran up to 120k requests per scenario (
arrayBuffer/json/text,clone, aborted reads, unread responses, double reads), with GC between rounds of 1,500. Memory levels off, andArrayBuffermemory returns to 0 every round. RSS grows in steps on double reads, but a pure-JS control that never touches impit grows the same way.Merge Danger
Door: two-way
No API or type changes; reverting the commit restores the old behaviour.
Blast Radius: body-reading
arrayBuffer()/bytes()/text()/json()call in the Node package goes through the new path.AbortSignalthat fires afterfetch()resolves but before the body is read now rejects the read. Before, the body was read anyway.ArrayBuffers (Electron) still work, but napi copies the body once more there.Closes #641