Repository navigation
feat(ecmascript): fold indexOf, lastIndexOf, and charCodeAt on strings with lone surrogates - #26758
Merged
Merged
Conversation
This was referenced Sep 17, 2026
This was referenced Sep 17, 2026
Member
Author
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 18, 2026 02:20
1e90e0d to
d29d07b
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
2 times, most recently
from
September 18, 2026 15:15
41783f8 to
fcd7b6a
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 18, 2026 15:15
d29d07b to
ca3e24e
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 21, 2026 01:14
d47b34a to
aa3edfe
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 21, 2026 01:14
ee9b3b7 to
3df1998
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 21, 2026 01:48
caada83 to
871acbe
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 21, 2026 01:48
9b3e65b to
9dbc8c1
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 21, 2026 01:54
871acbe to
bbae803
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
2 times, most recently
from
September 21, 2026 02:54
ba93a56 to
d1f0d04
Compare
6 of 22 tasks
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 21, 2026 07:04
d1f0d04 to
e97456f
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 21, 2026 07:04
5aeddda to
c7e9085
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 21, 2026 07:58
699e62c to
d00abc0
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
2 times, most recently
from
September 21, 2026 08:23
a4e8230 to
cdb3abb
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
2 times, most recently
from
September 22, 2026 02:41
89d5b6b to
afcd5cc
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 22, 2026 02:41
cdb3abb to
4013fd9
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 22, 2026 04:12
afcd5cc to
3ff125e
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
2 times, most recently
from
September 22, 2026 08:20
761e789 to
5c24f47
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 22, 2026 08:20
3ff125e to
34cb13e
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 23, 2026 01:55
5c24f47 to
a92bf91
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
2 times, most recently
from
September 23, 2026 02:19
569c770 to
f4a3d1f
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
2 times, most recently
from
September 23, 2026 02:24
805fbe7 to
fde800f
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 23, 2026 02:24
f4a3d1f to
fcfc5de
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 23, 2026 02:55
fde800f to
5c35c20
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
2 times, most recently
from
September 23, 2026 03:14
0e99bb0 to
b9bffc3
Compare
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
2 times, most recently
from
September 23, 2026 07:22
68650e3 to
f6d2e81
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 23, 2026 07:22
b9bffc3 to
cada6b1
Compare
…s with lone surrogates
A string literal containing a lone surrogate has no UTF-8
representation, so every string method fold declined it and calls like
"a\uD800b".indexOf("b") or "a\uD800b".charCodeAt(1) survived
minification. The numeric folds do not need a UTF-8 receiver: indexOf,
lastIndexOf, and charCodeAt return positions and code units, which are
defined for any sequence of UTF-16 code units.
Implement StringIndexOf, StringLastIndexOf, and StringCharCodeAt for
JSStr. A value without lone surrogates delegates to the existing UTF-8
implementations. Otherwise the search compares UTF-16 code units
directly, the domain the specification defines, so a lone surrogate
counts as one unit and no boundary rounding is needed. JSStr keeps its
WTF-8 bytes private, which rules out the byte search the UTF-8 path
uses; the unit walk iterates the encoded units without allocating and
only runs on the rare lone-surrogate literals. A search value with a
lone surrogate still declines, since constant string values remain
UTF-8. charAt also still declines: its result is a string, which may
have no UTF-8 form.
Verified against node on a generated corpus of 4382 string method
calls over lone and paired surrogate values, with identical program
output, byte-identical double minification, and unchanged minifier
conformance snapshots.
Part of #26242.
Implemented with AI assistance.
Dunqing
force-pushed
the
jsstr-utf16-string-folds
branch
from
September 27, 2026 06:37
f6d2e81 to
8d1a725
Compare
Dunqing
force-pushed
the
jsstr-surrogate-numeric-folds
branch
from
September 27, 2026 06:37
cada6b1 to
5980384
Compare
Base automatically changed from
jsstr-utf16-string-folds
to
jsstr-estree-escape-table
September 27, 2026 06:37
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.

Continued in #27097. GitHub marked this PR indirectly merged during stack reordering; the change has not landed in
main.The
indexOf,lastIndexOf, andcharCodeAtfolds read the receiver throughJSStr::as_str, so any string literal containing a lone surrogate declined to fold. These methods work in UTF-16 code units, where a lone surrogate is an ordinary unit:"a\uD800b".indexOf("b")is 2 and"a\uD800b".charCodeAt(1)is 55296, but both survived minification as calls. Both esbuild and terser fold thecharCodeAtcase.Implement the existing
StringIndexOf,StringLastIndexOf, andStringCharCodeAttraits forJSStrand drop the receiver gate at those three fold sites. A value without lone surrogates delegates to the&strimplementation unchanged. Otherwise the search compares UTF-16 code units through theencode_utf16iterator, which allocates nothing and needs no boundary rounding; this path only runs on the rare literals that contain a lone surrogate. The search value still arrives as UTF-8 throughConstantValue, so a search value containing a lone surrogate keeps declining; supporting those is the follow-up that movesConstantValueitself toJSStr.Verified against node on a generated corpus of 4,382 calls over lone-surrogate, astral, and mixed receivers: minified and original programs produce identical output, minify-twice is byte-identical, and minifier conformance and size snapshots are unchanged. On that corpus
charCodeAtnow folds completely andindexOf/lastIndexOffold every case whose search value is representable.The next PR restructures these implementations when the search value itself becomes
JSStr; taking the receiver side first keeps each behavior change reviewable on its own.Tenth PR in the JSStr stack, based on #26752. Part of #26242.
Implemented with AI assistance.