Repository navigation
Strip whitespace in linear time - #36
Conversation
The regexes that find trailing whitespace in whitespace control were retried at every position of a whitespace run, which takes quadratic time on long runs. They are now anchored so that matching takes linear time. Ported from handlebars.js 812c226.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The optimized regexes preserve existing behavior while addressing the performance issue with focused regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Anchors whitespace-control regexes to ensure linear-time parsing of long whitespace runs.
Changes:
- Optimizes trailing-whitespace detection and removal.
- Adds regression tests for whitespace stripping, standalone detection, and partial indentation.
| File | Description |
|---|---|
lib/whitespace-control.js |
Anchors whitespace regexes to avoid quadratic retries. |
spec/ast.js |
Adds large-input performance regression tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
any benches you have with either approach? |
@NullVoxPopuli Note that this is mainly to prevent users from doing DoS attacks on a Node.js backend with a template that takes forever to parse. I tasked Opus 5.5 to bench it: Each template was parsed with the full
One bad template is enough to stall the event loop: a 100 KB input of mostly spaces took about 12 seconds to parse with the old code. Bench
|
The regexes that find trailing whitespace in whitespace control were retried at every position of a whitespace run, which takes quadratic time on long runs. They are now anchored so that matching takes linear time.
Ported from handlebars.js 812c226.