(csharp, javascript, typescript, yaml) Quadratic time on blank lines, word runs and long lines (11.12.0)
Describe the issue
Three more regexes in 11.12.0 take quadratic time on very plain input: a block of empty lines (javascript, typescript), a run of words with no ( (csharp), and one long line with no : (yaml). It is the same class as #4362 and #4529; none of these three is in the #4529 list, and they are still on main (35f98874f9).
Each one was checked twice: by timing the regex on its own, and by changing only that regex in the built highlight.min.js and timing hljs.highlight() again.
grammar (src/languages) |
regex | input | 10 KB | 20 KB | 40 KB | 40 KB after the change below |
|---|---|---|---|---|---|---|
typescript (typescript.js:55) |
USE_STRICT |
"\n".repeat(n) |
75 ms | 283 ms | 1358 ms | <1 ms |
javascript (javascript.js:344) |
USE_STRICT |
"\n".repeat(n) |
71 ms | 293 ms | 1151 ms | <1 ms |
csharp (csharp.js:374) |
function begin |
"ab ".repeat(n / 3) |
317 ms | 1249 ms | 4846 ms | 6 ms |
yaml (yaml.js:25) |
KEY |
"ab ".repeat(n / 3) |
23 ms | 71 ms | 284 ms | 36 ms |
Time roughly quadruples when the input doubles. Larger inputs get long: 100 KB of ab ab ab … in 80-character lines took 52 s in csharp, and 200,000 empty lines took about 30 s in typescript.
Real code is not affected in csharp and yaml: in C# a ( turns up every few words, and YAML lines are short. The inputs that hit it are prose or a word list in a .cs file or a csharp code fence, a one-line YAML document, and a file with a long run of blank lines.
Which language seems to have the issue?
javascript, typescript, csharp, yaml.
Are you using highlight or highlightAuto?
highlight, with the language given explicitly.
Sample Code to Reproduce
// npm i [email protected]
const hljs = require("highlight.js");
const cases = [
["typescript", n => "\n".repeat(n)],
["javascript", n => "\n".repeat(n)],
["csharp", n => "ab ".repeat(n / 3)],
["yaml", n => "ab ".repeat(n / 3)],
];
for (const [language, make] of cases) {
for (const n of [10000, 20000, 40000]) {
const t = process.hrtime.bigint();
hljs.highlight(make(n), { language });
console.log(language, n, Number(process.hrtime.bigint() - t) / 1e6, "ms");
}
}
The numbers in the table are from highlight.min.js of @highlightjs/cdn-assets 11.12.0, Node v26.12.1, Apple Silicon.
What is going on, and the change I tested
-
USE_STRICTinjavascript.jsandtypescript.js:begin: /^\s*['"]use (strict|asm)['"]/^matches at the start of every line, and\s*then runs across all the blank lines that follow before failing. N empty lines cost O(N²). Tested change:[ \t]*in place of\s*. A directive preceded by blank lines still matches, starting at its own line. -
The
functionmode incsharp.js:begin: '(' + TYPE_IDENT_RE + '\\s+)+' + hljs.IDENT_RE + '\\s*(<[^=]+>\\s*)?\\(',In a run of whitespace-separated identifiers every identifier is a start position, and the group consumes the rest of the run (
\s+crosses newlines) before finding no(. Tested change: bound the repetition,{1,8}in place of the outer+. I have not run the csharp markup tests against it. The<[^=]+>part is a second unbounded scan ("a b<".repeat(n / 4): 11 / 45 / 179 ms), much milder and not changed by the bound. -
KEYinyaml.js:{ begin: /[\w*@][\w*@ :()\./-]*:(?=[ \t]|$)/ },On a line with no
:every position scans to the end of the line. Tested change:{0,255}in place of*, which leaves keys longer than 256 characters unhighlighted. The two quoted variants on the next lines have the same shape.
Expected behavior
Time that grows linearly with the input for these inputs.
Additional context
Found while measuring how long highlight() can block a page on untrusted text. The measurements and this write-up were done with the help of Claude Code; every number above is from an actual run.
Two more with the same ^\s* shape as USE_STRICT, found by running a block of empty lines through the other grammars I use (11.12.0, same setup as above):
grammar (src/languages) |
regex | input | 10 KB | 20 KB | 40 KB | 40 KB after the change |
|---|---|---|---|---|---|---|
ruby (ruby.js:423) |
begin: /^\s*=>/ |
"\n".repeat(n) |
174 ms | 565 ms | 2231 ms | 1 ms |
csharp (csharp.js:354) |
begin: '^\\s*\\[(?=[\\w])' |
"\n".repeat(n) |
155 ms | 732 ms | 2508 ms | <1 ms |
Tested change in both: [ \t]* in place of \s*, checked the same way (only that regex changed in the built highlight.min.js, then hljs.highlight() timed again).
The other grammars I checked on the same input stayed under 50 ms at 40 KB: python, java, go.