Skip to content

stream: avoid intermediate Blob in stream/consumers arrayBuffer() - #67714

Open
ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:perf/stream-consumers-arraybuffer
Open

ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:perf/stream-consumers-arraybuffer

Conversation

@ychuhai

@ychuhai ychuhai commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

stream/consumers arrayBuffer() (and buffer() / bytes()) currently wraps chunks in a temporary Blob and reads them back out, copying the data twice and queuing a microtask per chunk

Concatenate Uint8Array / Buffer chunks directly into an ArrayBuffer instead, falling back to Blob for other chunk types

benchmark/streams/consumers.js, 30 runs:

case improvement confidence
arrayBuffer, buffer, 4 chunks +159.65% ***
arrayBuffer, buffer, 32 chunks +350.76% ***
buffer, buffer, 4 chunks +159.71% ***
buffer, buffer, 32 chunks +346.79% ***
bytes, buffer, 4 chunks +154.51% ***
bytes, buffer, 32 chunks +360.24% ***

In node:stream/consumers, arrayBuffer() (also used by buffer() and
bytes()) collected stream chunks into a temporary Blob and then read
them back out asynchronously via blob.arrayBuffer(). For binary
streams this copied every chunk twice, allocated an intermediate copy,
and queued a microtask per chunk.

Add a fast path that concatenates chunks directly into a single
ArrayBuffer when all chunks are non-resizable ArrayBuffer-backed
Uint8Arrays (including Buffers) within kMaxLength, falling back to
Blob for all other chunk types. Behavior is unchanged.

Signed-off-by: Yaroslav Chuhai <yaroslavchugai@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance
  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@Renegade334

Copy link
Copy Markdown
Member

Thanks for the contribution.

We already have complete implementations of these exact consumers in the stream/iter API, which we should be able to use as drop-in replacements rather than duplicating everything:

const { from } = require('internal/util/streams/iter/from');
const { arrayBuffer: arrayBufferImpl } = require('internal/util/streams/iter/consumers');

function arrayBuffer(stream) {
  return arrayBufferImpl(from(stream));
}

// ...

@ychuhai

ychuhai commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution.

We already have complete implementations of these exact consumers in the stream/iter API, which we should be able to use as drop-in replacements rather than duplicating everything:

const { from } = require('internal/util/streams/iter/from');
const { arrayBuffer: arrayBufferImpl } = require('internal/util/streams/iter/consumers');

function arrayBuffer(stream) {
  return arrayBufferImpl(from(stream));
}

// ...

Thanks for the review! I tried switching to internal/streams/iter and running test test/parallel/test-stream-consumers.js and it has a few compatibility issues:

  • from(readable) throws ERR_STREAM_ITER_MISSING_FLAG unless --experimental-stream-iter is passed
  • even with the flag, existing objectMode tests in test-stream-consumers.js fail with ERR_INVALID_ARG_TYPE (and Blob chunks fail too), because stream/consumers relies on Blob converting arbitrary values to strings.
  • iter's text() uses fatal: true, which breaks the existing invalid UTF-8 tests. It also accepts SharedArrayBuffer / resizable ArrayBuffer views (which Blob rejects) and doesn't copy single-chunk buffers.

@Renegade334 Renegade334 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.

Fair enough, thanks for checking.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.36066% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.55%. Comparing base (db671bb) to head (745c029).
⚠️ Report is 126 commits behind head on main.

Files with missing lines Patch % Lines
lib/stream/consumers.js 98.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67714      +/-   ##
==========================================
- Coverage   90.71%   90.55%   -0.16%     
==========================================
  Files         801      801              
  Lines      280917   284178    +3261     
  Branches    54237    54891     +654     
==========================================
+ Hits       254832   257351    +2519     
- Misses      16253    16997     +744     
+ Partials     9832     9830       -2     
Files with missing lines Coverage Δ
lib/stream/consumers.js 99.35% <98.36%> (-0.65%) ⬇️

... and 85 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.

@Ethan-Arrowood Ethan-Arrowood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

excellent!

@Ethan-Arrowood Ethan-Arrowood added 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 23, 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 23, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@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 the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Nov 23, 2026

@gurgunday gurgunday 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

@Renegade334 Renegade334 added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants