Skip to content

perf: close residual O(n²) parse time on unmatched emphasis/strikethrough delimiter runs (follow-up to #4099 / #4103) - #4196

Merged
UziTech merged 2 commits into
markedjs:masterfrom
VadimToptunov:fix/4099-residual-double-delim
Nov 16, 2026
Merged

UziTech merged 2 commits into
markedjs:masterfrom
VadimToptunov:fix/4099-residual-double-delim

Conversation

@VadimToptunov

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #4099 (closed by #4103 in marked 18.1.3). PR #4103's cached lastPossibleCloserIndex correctly makes Tokenizer.emStrong and Tokenizer.del O(1) when no closer exists ahead, but the outer Lexer#tokenizeInlines loop still advances one character at a time through a run of same-char delimiters, so the greedy ^\*+ anchor in emStrongLDelim re-scans the whole run on every outer position. The inline pass remains O(n²) on pathological inputs even though each inner closer scan is O(1).

Measured on current master:

input time
'**'.repeat(20000) + 'x' 8.73 s
'__'.repeat(20000) + 'x' 11.30 s
'*'.repeat(40000) + 'x' 8.73 s
'_'.repeat(40000) + 'x' 11.30 s
'~'.repeat(40000) + 'x' (similar)

With this change, the same inputs render in ~0.001 s on the same machine (>4000× speedup, O(n²) → O(n)). Rendered HTML output is unchanged — these inputs have always rendered as literal text; only the time to reach that result changes.

The change

Before calling emStrong / del, check the same cached lastPossibleEmStrongCloserIndex / lastPossibleDelCloserIndex that #4103 already maintains. If no closer for the current delim character exists at or after the current position in maskedSrc, the entire run of same-delim characters cannot be emphasis / strong / strikethrough; emit the whole run as literal text in one step and advance past it.

The fix lives in Lexer#tokenizeInlines right before the emStrong / del tokenizer calls. It is 40 lines (with duplication for ~), adds no new exports beyond re-using the two existing cached helpers, and does not touch regex sources or the Tokenizer emphasis logic.

Tests

  • The full CommonMark spec suite passes (1983 tests).
  • The project's unit tests pass (242 tests).
  • A new fixture test/specs/new/em_strong_unmatched_delim_run.md documents the behavior for closer-less *, _, and ~ runs. These render as paragraph text before and after the change.

Why

Reported on 18.1.3 as a residual quadratic in the same class as #4099 — the **/__/~~ double-delimiter forms and the */_ single-delimiter forms. The lastPossibleCloser cache that #4103 added is exactly the signal needed to detect these runs, so the fix reuses existing infrastructure.

I understand marked's position per the SECURITY.md pointer to using web-worker isolation for untrusted input (which is correct and I have no disagreement with); this PR is purely a correctness / perf cleanup, not framed as a security fix.

Methodology note: this was reduced from an adversarial-opener fuzz run targeting markdown libraries; the perf curve was measured manually before I wrote any code, and the fix was verified against the CommonMark spec suite.

Follow-up to markedjs#4099 / markedjs#4103. When an input line contains a long run of `*`,
`_` or `~` with no closer anywhere ahead in maskedSrc, every opener
position in Tokenizer.emStrong / Tokenizer.del was correctly early-returning
via the lastPossibleCloserIndex guard introduced by markedjs#4103, but the outer
inline loop still advanced one character at a time through the run. The
`^\*+` greedy anchor in emStrongLDelim re-scans the whole run on each
call, so the inline pass remains O(n^2) even though the inner closer scan
is now O(1).

Measured on master before this change:

```
**` x 20000 + 'x'   8.73 s
__` x 20000 + 'x'  11.30 s
*`  x 40000 + 'x'   8.73 s
_`  x 40000 + 'x'  11.30 s
```

With this change the same inputs render in ~0.001 s. The CommonMark
spec test suite (1983 cases) and the project's unit tests (242 cases)
continue to pass. A new fixture `em_strong_unmatched_delim_run`
documents the behavior for closer-less `*`, `_`, and `~` runs.

The fix checks the cached last-possible-closer index that markedjs#4103
already maintains; when no closer exists after the current position,
the entire run of same-delim characters is consumed as literal text in
one step. Behavior is unchanged (runs without a closer have always
rendered as literal text); only the time to reach that result changes.
@vercel

vercel Bot commented Nov 7, 2026

Copy link
Copy Markdown

@VadimToptunov is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marked-website Ready Ready Preview Nov 10, 2026 4:45pm UTC

Request Review

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test in test/specs/redos that fails without this?

Mirrors the quadratic_em_mask.cjs / quadratic_br.cjs style. Each of the
four cases fails on 18.1.3 baseline (times out around 2.3-3.0s per case
in test:specs) and passes in 5-7ms with the fix.

- '**' x 10000 + 'x' -> <p>**...**x</p>
- '__' x 10000 + 'x' -> <p>__...__x</p>
- '*'  x 20000 + 'x' -> <p>*...*x</p>
- '_'  x 20000 + 'x' -> <p>_..._x</p>

Requested by @UziTech in review of markedjs#4196.

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 💯

@UziTech
UziTech merged commit 0b8b3b3 into markedjs:master Nov 16, 2026
8 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 16, 2026
## [18.2.1](v18.2.0...v18.2.1) (2026-11-16)

### Bug Fixes

* allow tabs between the tag name and attributes of an html block ([#4207](#4207)) ([738ebeb](738ebeb))
* **cli:** wait for stdout before exiting ([#4197](#4197)) ([b535046](b535046))
* continue a block quote paragraph with a lazy line that starts an html tag ([#4202](#4202)) ([8a578e4](8a578e4))
* end a block quote at a lazy line after an empty list item ([#4178](#4178)) ([e2f77b4](e2f77b4))
* end a list item before a lazy line after an empty or non-paragraph nested block ([#4183](#4183)) ([ac2256e](ac2256e))
* Fix bracket escapes inside code spans in link text being stripped ([#4205](#4205)) ([9a26ae9](9a26ae9)), closes [#4176](#4176) [#4176](#4176) [#4176](#4176)
* keep a tab after the block quote marker's space literal ([#4208](#4208)) ([2d3b47f](2d3b47f))
* keep rejected table candidates in the preceding paragraph ([#4218](#4218)) ([1c5969e](1c5969e))
* keep tabs that are code content under a block quote ([#4217](#4217)) ([d2c6f73](d2c6f73)), closes [#4184](#4184)
* let a mid-run opener close against a delimiter that can also open ([#4203](#4203)) ([06e1d10](06e1d10))
* let an autolink or inline html line start or continue a setext heading ([#4191](#4191)) ([72d0fec](72d0fec))
* parse links generated by inline tokenizers ([#4204](#4204)) ([0460f98](0460f98))
* preserve trailing blank lines in fenced code ([#4200](#4200)) ([7499eb3](7499eb3))
* reject a link destination in angle brackets with an unescaped < or > ([#4206](#4206)) ([de71210](de71210))
* remove a task item's checkbox marker from its own text ([#4215](#4215)) ([5902442](5902442))
* **types:** preserve custom output types in tokenizer overrides ([#4214](#4214)) ([5e35708](5e35708))

### Performance Improvements

* check the first character before inline tokenizer regexes ([#4193](#4193)) ([95ccf6c](95ccf6c))
* close residual O(n²) parse time on unmatched emphasis/strikethrough delimiter runs (follow-up to [#4099](#4099) / [#4103](#4103)) ([#4196](#4196)) ([0b8b3b3](0b8b3b3))
* skip the emphasis closer scan for inline runs without a delimiter ([#4195](#4195)) ([ffa06d5](ffa06d5))

This branch was successfully deployed

1 active deployment
Preview — e955fe97 Deployed Nov 10, 2026 by vercel[bot]
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.

3 participants