typescript.regex_compile
Performance
High
Detects regex compilation inside loops or hot paths.
Why It Matters
Section titled “Why It Matters”Regex in loops:
- Parses repeatedly — Same pattern compiled per iteration
- Wastes CPU — 10-100x slower than cached
- Hurts latency — Measurable request delays
Example
Section titled “Example”// ❌ Before (compiling in loop)for (const line of lines) { const match = new RegExp('\\d+').exec(line); // Compiles every iteration! if (match) { count++; }}// ✅ After (compile once)const numberRegex = /\d+/;
for (const line of lines) { if (numberRegex.test(line)) { count++; }}What Unfault Detects
Section titled “What Unfault Detects”- new RegExp() inside loops
- Regex compilation in request handlers
- Dynamic regex patterns that could be static
Auto-Fix
Section titled “Auto-Fix”Unfault can hoist regex to module level constants.