Repeated Regex in Loop¶
Rule details
| Rule ID | repeated-regex-in-loop |
| Severity | WARNING — likely performance problem |
| Confidence | HIGH — structurally proven |
| Category | Loop amplifiers |
| Complexity | O(n·compile) → O(n) |
Description¶
Detects regex pattern compilation inside loops. Compiling a regular expression is expensive — doing it on every iteration wastes CPU cycles when the compiled pattern could be reused.
Typical¶
After¶
Suggestion¶
Compile the pattern once outside the loop and reuse the compiled Pattern