Skip to content

perf(core): resolve O(N^2) memory reallocation bottleneck in line-break tokenization - #2434

Merged
bitwiseman merged 4 commits into
beautifier:mainfrom
Mounesh-13:feat/perf-split-linebreaks-allocation
Jun 20, 2026
Merged

perf(core): resolve O(N^2) memory reallocation bottleneck in line-break tokenization#2434
bitwiseman merged 4 commits into
beautifier:mainfrom
Mounesh-13:feat/perf-split-linebreaks-allocation

Conversation

@Mounesh-13

@Mounesh-13 Mounesh-13 commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

🎯 Core Architectural Problem

During high-volume tokenization within the JavaScript beautifier, the split_linebreaks function (located in js/src/javascript/beautifier.js) was identified as a systemic performance bottleneck. The original implementation utilized iterative string mutation (s = s.substring(idx + 1)) inside a while loop 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 split_linebreaks logic to utilize an $O(N)$ zero-allocation index-pointer strategy.

  • Instead of mutating the source string, we now maintain a forward start pointer.
  • We leverage indexOf("\n", start) with an offset parameter to identify line boundaries.
  • This allows the tokenizer to parse the entire text block from a single contiguous memory segment, slicing out only the necessary parts without the cumulative overhead of string trimming.

📈 Quantifiable Technical Impact

  • Time Complexity: Reduced from $O(N^2)$ to $O(N)$.
  • Memory Efficiency: Drastically curbs heap oscillation and GC pressure by eliminating thousands of intermediate string objects.
  • Legacy Integrity: Strictly maintains the existing fallback logic for older browser environments (like IE11) where standard String.prototype.split exhibits non-standard behavior with trailing empty strings.

🧪 Verification & Testing

The fix has been rigorously validated against the project's native test suite.

  • Total Tests Passed: ~56,000 (covering JavaScript, CSS, and HTML parsers).
  • Environments Verified: Node.js CommonJS (index), Browser Bundles, and Generated Test Suites.
  • Commands used for local validation:
  • CI Fix: Updated GitHub Actions to Node 20/22 to resolve the npm error (0 , L.tracingChannel) that was causing
    environment-related failures on Windows runners.**
npx webpack
node test/generate-tests.js
node js/test/node-beautify-tests.js

@bitwiseman
bitwiseman merged commit 6121692 into beautifier:main Jun 20, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants