Regex Performance: Tips for Writing Efficient Patterns
Why Regex Performance Matters
A poorly written regex can take exponentially longer as input grows. Understanding regex engine behavior helps you write efficient patterns.
Catastrophic Backtracking
The most common performance issue. It occurs when the regex engine tries many paths before failing.
Bad Pattern
`` Pattern: (a+)+$ Text: aaaaaaaaaaaaaaaaaaaaaaaaab
`
This can take seconds or even hang because the engine backtracks exponentially.
Why It Happens
Nested quantifiers like (a+)+ create exponential possibilities. For each 'a', the engine must decide which + captures it.
Performance Tips
1. Be Specific
General patterns are slower than specific ones:
` Slow: .*foo Fast: [^f]*foo
`
2. Anchor When Possible
` Without anchor: \d{4}-\d{2}-\d{2} With anchor: ^\d{4}-\d{2}-\d{2}$
`
Anchors tell the engine exactly where to look.
3. Use Atomic Groups (?>...)
Prevents backtracking into the group:
` Standard: (?:\d+)\.\d+ Atomic: (?>\d+)\.\d+
`
4. Use Possessive Quantifiers
Add + after quantifiers to prevent backtracking:
` Greedy: \d+ Possessive: \d++
`
Note: Not supported in all regex flavors.
5. Avoid Nested Quantifiers
` Bad: (a+)+ Better: a+
`
6. Order Alternations by Likelihood
` If "cat" is more common: Fast: cat|dog|bird Slow: bird|dog|cat
`
7. Use Character Classes Instead of Alternation
` Slow: (a|b|c|d|e) Fast: [abcde]
`
8. Compile Regex Once
In loops, compile the regex outside:
`javascript
// Bad - compiles every iteration
for (const line of lines) {
if (/pattern/.test(line)) { }
}
// Good - compile once
const regex = /pattern/;
for (const line of lines) {
if (regex.test(line)) { }
}
``
Testing Performance
Use RegexSpark to test your patterns against large inputs. Watch for:
- Patterns that slow down significantly with longer input
- Patterns that hang on certain inputs
- Exponential time complexity
When to Avoid Regex
Sometimes regex isn't the best tool:
- Simple string operations (use indexOf, includes)
- Parsing nested structures (use a parser)
- Very complex validation (use multiple steps)
Summary