Skip to content

buffer: add buffer.stringLength() - #66064

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:buffer-string-length
Open

mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:buffer-string-length

Conversation

@mcollina

@mcollina mcollina commented Sep 16, 2026

Copy link
Copy Markdown
Member

Add buffer.stringLength(input[, encoding]), the counterpart of Buffer.byteLength(): it returns the number of UTF-16 code units that buf.toString(encoding) would produce, without decoding.

For UTF-8 the count is computed with simdutf. Invalid input is counted with the same maximal-subpart replacement the decoder applies, so the result always matches toString().length. The other encodings are computed from byteLength alone.

This lets code that accumulates streamed input check the result against buffer.constants.MAX_STRING_LENGTH and size its memory budget before decoding, as discussed in #66062.

Throughput on x64 (benchmark/buffers/buffer-stringlength.js, utf8):

input 4 KiB 1 MiB
ascii 3.5M ops/s ~17 GB/s
multibyte valid 1.7M ops/s ~7.5 GB/s
invalid at the end 1.6M ops/s ~7.5 GB/s

Refs: #66062

--

AI generated. Humanly reviewed.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 16, 2026
Add `buffer.stringLength(input[, encoding])`, the counterpart of
`Buffer.byteLength()`: it returns the number of UTF-16 code units that
`buf.toString(encoding)` would produce, without decoding.

For UTF-8 the count is computed with simdutf. Invalid input is counted
with the same maximal-subpart replacement that the decoder applies, so
the result always matches `toString().length`. The other encodings are
computed from `byteLength` alone.

This lets code that accumulates streamed input check the result against
`buffer.constants.MAX_STRING_LENGTH` and size its memory budget before
decoding.

Refs: nodejs#66062
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the buffer-string-length branch from ae5bfce to b1c6e2b Compare September 16, 2026 13:18
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.42553% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.27%. Comparing base (7aaf9b4) to head (b1c6e2b).
⚠️ Report is 438 commits behind head on main.

Files with missing lines Patch % Lines
src/node_buffer.cc 86.15% 5 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66064      +/-   ##
==========================================
+ Coverage   90.13%   90.27%   +0.13%     
==========================================
  Files         751      789      +38     
  Lines      253639   271568   +17929     
  Branches    47790    51833    +4043     
==========================================
+ Hits       228618   245156   +16538     
- Misses      16264    16880     +616     
- Partials     8757     9532     +775     
Files with missing lines Coverage Δ
lib/buffer.js 99.74% <100.00%> (+0.59%) ⬆️
src/node_buffer.cc 70.23% <86.15%> (+0.30%) ⬆️

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

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

Labels

buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants