DEV Community

Ship Stack
Ship Stack

Posted on Originally published at youtube.com

Can one regex freeze your Node server?

A single regex can pin a Node process for seconds. Not because the input is big, but because the pattern gives the engine too many ways to fail. Here is the measured version.

The test

const re = /(a+)+$/;

function timeIt(n) {
  const s = 'a'.repeat(n) + '!';
  const t0 = process.hrtime.bigint();
  const result = re.test(s);
  const t1 = process.hrtime.bigint();
  return { n, result, ms: Number(t1 - t0) / 1e6 };
}

for (const n of [20, 22, 24, 26, 28]) console.log(timeIt(n));
Enter fullscreen mode Exit fullscreen mode

Every string is non-matching, because of the trailing !. On a real run in Node.js 22 the same machine produced:

{ n: 20, result: false, ms: 79.7 }
{ n: 22, result: false, ms: 47.9 }
{ n: 24, result: false, ms: 192.6 }
{ n: 26, result: false, ms: 766.5 }
{ n: 28, result: false, ms: 3092.2 }
Enter fullscreen mode Exit fullscreen mode

The video run of this same test measured 40 ms at 20 letters, 100 ms at 24, 413 ms at 26 and 1592 ms at 28. The absolute numbers move with the machine. The shape does not: each two extra letters roughly doubles to quadruples the time.

Why

(a+)+ can match the same run of a characters in many different ways. The outer + can take one inner group of 28, or a group of 27 then 1, or 14 then 14, and so on. On a matching string the engine finds a split immediately. On a non-matching string it has to try every split before it can say no, and the number of splits grows exponentially with the length of the run.

That is catastrophic backtracking. At 28 characters it is already three seconds of blocked event loop. At 32 it would be minutes. Node is single-threaded, so every other request waiting on that process waits with it.

The fix

Make the pattern unambiguous. If you only mean "one or more a, anchored at the end", write that:

const safe = /^a+$/;
safe.test('a'.repeat(28) + '!'); // false in 0.035 ms
Enter fullscreen mode Exit fullscreen mode

Rules that remove the ambiguity:

  • Drop the nested quantifier. (a+)+ and (\w+)* are the usual culprits.
  • Anchor the pattern and keep it linear where you can.
  • When a nested quantifier is genuinely needed, make it atomic or possessive if the engine supports it, or rewrite it as a single character class.
  • Keep regexes that run on user input out of the hot path, and if you must run an untrusted pattern, run it in a worker with a timeout so a slow one cannot block the whole process.

What it looks like in production

The symptom is not an error. It is a process that stops answering for a few seconds under a particular input, CPU pegged at 100% on one core, then recovers. That is why these bugs survive so long: nothing throws, and the input that triggers it looks ordinary.

How this was tested

Real runs in Node.js 22 with process.hrtime.bigint() around each re.test. The narration in the video is an AI-generated voice; every number above came from a terminal. Short video with the same test: https://youtube.com/shorts/WWfCDJ1gxqY

Top comments (1)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

Node 22 has a one-flag safety net for this, though it's marked experimental. With --enable-experimental-regexp-engine-on-excessive-backtracks your (a+)+$ at 28 letters went from about 4 seconds to under a millisecond when I ran it. It won't save lookarounds or backreferences though: adding (?=a) or \1 still took 3.5 to 7 seconds.