fix(scanner): make esprima2 string/template scanning linear - #15
Merged
Conversation
esprima2 builds string and template literal values with `value += ch` in a while loop (Scanner.scanStringLiteral / scanTemplate). In CPython that misses the in-place string-concat optimization, so each append copies the whole buffer and scanning a single literal is O(n^2). Source that is one large string literal (e.g. a multi-megabyte base64 blob) takes ~70s to parse. Add pyjsclear/esprima_patch.py, applied at import of pyjsclear.parser. It takes the installed scanner method source and applies the two-line fix proposed upstream (s0md3v/esprima2#10): accumulate into a list and join once at the end. Deriving from the live source keeps it faithful to whatever esprima2 is installed; gated to verified versions (5.0.1, 5.0.2, 6.0.0), and it bails (leaving esprima untouched) if the source or version is unexpected. Parse of a 3.13MB single-literal file drops from ~70s to ~0.4s. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
itamarga
force-pushed
the
fix/esprima-quadratic-string-scan
branch
from
August 26, 2026 13:45
340bd1b to
eeab684
Compare
almogch
approved these changes
Aug 26, 2026
Merged
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.
esprima2 builds string and template literal values with
value += chinside a while loop with manual indexing (Scanner.scanStringLiteral / Scanner.scanTemplate). In CPython that loop shape misses the in-place string-concat optimization, so each append copies the whole buffer, making the scan O(n^2) in the length of a single literal. Source that is one large string literal (e.g. a multi-megabyte base64 blob) takes ~70s to parse.Add pyjsclear/esprima_patch.py, applied at import of pyjsclear.parser: it replaces both methods with faithful list-append + join reimplementations (character-for-character identical logic, linear accumulation). Version-gated to the esprima2 releases whose scanner source was verified against these ports (5.0.1, 5.0.2, 6.0.0 — identical on Python 3, where 5.x's uchr is chr); any other version leaves esprima untouched.
Parse of a 3.13MB single-literal file drops from ~70s to ~0.4s.