Conversation
Collaborator
|
Review requested:
|
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
force-pushed
the
buffer-string-length
branch
from
September 16, 2026 13:18
ae5bfce to
b1c6e2b
Compare
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
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.
Add
buffer.stringLength(input[, encoding]), the counterpart ofBuffer.byteLength(): it returns the number of UTF-16 code units thatbuf.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 frombyteLengthalone.This lets code that accumulates streamed input check the result against
buffer.constants.MAX_STRING_LENGTHand size its memory budget before decoding, as discussed in #66062.Throughput on x64 (
benchmark/buffers/buffer-stringlength.js, utf8):Refs: #66062
--
AI generated. Humanly reviewed.