#4099·marked

emStrong 与未匹配的强调分隔符数量成正比 (12 KB 的输入需要 ~16 秒)

作者: Darkslayer3324j创建于 2026年9月19日更新于 2026年9月20日
标签L2 - annoying

标记版本: 18.0.13(也为 main 版本,hash 为 4eb2d97)Markdown 格式: n/a(默认 / GFM)描述解析时间与未匹配的 * / _ 分隔符数量成正比,因此一个小的、精心编写的输入会冻结解析器。一个 12 KB 的文档在我的机器上大约需要 16-19 秒(Windows 11、Node 24;该机器正在运行一些任务,因此请对绝对数字保持松散,但增长速度才是重点):

javascript
import { marked } from 'marked';

for (const unit of ['- *', '+ _', '*x *x ']) {
  for (const n of [500, 1000, 2000, 4000]) {
    const src = unit.repeat(n);
    const t = performance.now();
    marked.parse(src);
    console.log(JSON.stringify(unit), src.length + 'B', Math.round(performance.now() - t) + 'ms');
  }
}
输入 1.5-3 KB 3-6 KB 6-12 KB 12 KB
'- *'.repeat(n) (n = 500..4000) 311 ms 1205 ms (x3.9) 5238 ms (x4.3) 19517 ms (x3.7)
'+ _'.repeat(n) 332 ms 995 ms (x3.0) 4001 ms (x4.0) 15726 ms (x3.9)
'*x *x '.repeat(n) (n = 500..2000) 893 ms 4111 ms (x4.6) 15993 ms (x3.9)

每次输入增加一倍时,时间大约增加 4 倍。使用 1.*、1)_、* _、- _ 等类似的输入也会表现出相同的行为,因此是重点处理而不是列表规则。没有列表标记的相同输入(' *_'.repeat(n))也是二次的。

内容来源: markedjs/marked