S7: Alert check O(n*m) loop over all signals per alert #12

Open
opened 2026-06-05 10:22:17 +00:00 by sebse · 0 comments
Owner

Code Smell

File: web/app/api/alerts/check/route.ts

The alert check endpoint has O(n*m) performance: for each active alert (n), it iterates over ALL latest signal values (m). With 50 alerts and 5000 latest signal rows, thats 250k iterations.

Code reference

for (const alert of alertsResult.rows) {
  for (const [key, vals] of latestMap) {
    // ... regex test for each combination
  }
}

Why it matters

The regex test (RegExp.test) and optional resolveName call inside the inner loop adds up. For regex alerts, the pattern is recompiled on every iteration (currently fine because its in the outer loop, but still heavy).

Suggestion

Pre-build a map keyed by item_key for exact-match alerts. For regex alerts, pre-compile the pattern. Consider partitioning the loop: exact matches first via direct map lookup, then regex matches.

Note

This code was flagged as low priority. The latestMap is typically small (< 100 entries) and alerts are < 20.

## Code Smell **File:** `web/app/api/alerts/check/route.ts` The alert check endpoint has O(n*m) performance: for each active alert (n), it iterates over ALL latest signal values (m). With 50 alerts and 5000 latest signal rows, thats 250k iterations. ### Code reference ```ts for (const alert of alertsResult.rows) { for (const [key, vals] of latestMap) { // ... regex test for each combination } } ``` ### Why it matters The regex test (`RegExp.test`) and optional `resolveName` call inside the inner loop adds up. For regex alerts, the pattern is recompiled on every iteration (currently fine because its in the outer loop, but still heavy). ### Suggestion Pre-build a map keyed by item_key for exact-match alerts. For regex alerts, pre-compile the pattern. Consider partitioning the loop: exact matches first via direct map lookup, then regex matches. ### Note This code was flagged as low priority. The `latestMap` is typically small (< 100 entries) and alerts are < 20.
sebse added the code-smell label 2026-06-05 10:22:17 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sebse/factorio-signal-exporter#12