Skip to content

perf(node): read buffered response bodies natively instead of chunk by chunk - #646

Merged
barjin merged 2 commits into
apify:masterfrom
st3ffgv4:perf/node-native-body-read
Dec 4, 2026
Merged

barjin merged 2 commits into
apify:masterfrom
st3ffgv4:perf/node-native-body-read

Conversation

@st3ffgv4

@st3ffgv4 st3ffgv4 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

arrayBuffer(), bytes(), text() and json() 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.arrayBuffer()            # bytes(), text(), json() all go through it
-  new Response(ReadableStream(bytes_stream()))
-    .arrayBuffer()                # 74 pieces → 74 crossings into JS
+  if body not yet a JS stream
+    select(abort signal, reqwest .bytes())
+    → one ArrayBuffer            # 1 crossing
+  else                            # .body or clone() was used
+    read the existing stream      # unchanged
  • response.body and clone() still stream as before.
  • The abort channel now exists from the start, so AbortSignal also stops a native read, including when it fires before the read starts.
  • A second read throws TypeError: Body is unusable: Body has already been read instead of reaching a panic!.
  • json() decodes as UTF-8 and strips a BOM, as Fetch does.

Evidence

The numbers below come from the repro.mjs benchmark script in #641, run unchanged except that it imports the local build instead of impit from npm. Both master and 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:

    impit  uncompressed  0.38–0.41
    impit  gzip          0.74–0.82
    

    After (this branch):

    impit  uncompressed  0.13–0.14
    impit  gzip          0.13–0.22
    

    Node's own fetch on the same gzip body: 0.62–0.83.

  • New tests:

    gzip body via arrayBuffer / bytes / text / json   == original bytes
    gzip body via .body                                arrives in >1 piece
    clone() + original on a gzip body                  both read it whole
    json() with a UTF-8 BOM                            parses
    arrayBuffer() twice / text() / .body after a read  TypeError
    abort, then arrayBuffer()                          rejects
    

    The full Node suite passes locally except Basic requests > to an http(s):// domain. That test fetches the live apify.com site, which refused connections from my local network. It fails the same way on master, 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, and ArrayBuffer memory 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

  • Every arrayBuffer()/bytes()/text()/json() call in the Node package goes through the new path.
  • An AbortSignal that fires after fetch() resolves but before the body is read now rejects the read. Before, the body was read anyway.
  • Runtimes that forbid external ArrayBuffers (Electron) still work, but napi copies the body once more there.

Closes #641

st3ffgv4 and others added 2 commits December 3, 2026 15:52
…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 barjin 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, thank you @st3ffgv4!

@barjin
barjin merged commit 9fe3c20 into apify:master Dec 4, 2026
34 checks passed
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.

Reading a gzip response costs 2-3x more JavaScript time than the same response uncompressed

2 participants