perf(core): resolve O(N^2) memory reallocation bottleneck in line-break tokenization - #2434
Merged
bitwiseman merged 4 commits intoJun 20, 2026
Conversation
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.
🎯 Core Architectural Problem
During high-volume tokenization within the JavaScript beautifier, the
split_linebreaksfunction (located injs/src/javascript/beautifier.js) was identified as a systemic performance bottleneck. The original implementation utilized iterative string mutation (s = s.substring(idx + 1)) inside awhileloop to parse segments.For large input files—particularly those with massive block comments or source-mapped text—this approach forces the V8 engine into a state of continuous memory reallocation. In algorithmic terms, this results in$O(N^2)$ time complexity relative to the string size, while simultaneously triggering high Garbage Collector (GC) churn as orphaned substring allocations are repeatedly trashed and collected.
🛠 Specific Optimized Solution
This PR refactors the$O(N)$ zero-allocation index-pointer strategy.
split_linebreakslogic to utilize anstartpointer.indexOf("\n", start)with an offset parameter to identify line boundaries.📈 Quantifiable Technical Impact
String.prototype.splitexhibits non-standard behavior with trailing empty strings.🧪 Verification & Testing
The fix has been rigorously validated against the project's native test suite.
environment-related failures on Windows runners.**