perf: optimize screen manager and respawn controller

- Pre-compile ANSI escape and whitespace regex patterns in respawn controller
- Pre-compile screen pattern regex for screen list parsing
- Batch ps and pgrep calls in getScreensWithStats() for multiple screens
- Reduces subprocess spawns from O(2n) to O(2) for n screens

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-19 11:49:59 +01:00
co-authored by Claude Opus 4.5
parent ce61a3b2f2
commit d0e5d3aed6
2 changed files with 72 additions and 10 deletions
+7 -2
View File
@@ -6,6 +6,10 @@ const MAX_RESPAWN_BUFFER_SIZE = 1024 * 1024;
// Keep this much when trimming (512KB)
const RESPAWN_BUFFER_TRIM_SIZE = 512 * 1024;
// Pre-compiled patterns for performance
const ANSI_ESCAPE_PATTERN = /\x1b\[[0-9;]*[HJKmsu?lh]/g;
const WHITESPACE_PATTERN = /\s+/g;
/**
* Respawn sequence states
*
@@ -194,9 +198,10 @@ export class RespawnController extends EventEmitter {
// Filter out noise - only count meaningful data as activity
// Ignore: cursor movements, color codes alone, small whitespace-only data
// Uses pre-compiled patterns for performance
const meaningfulData = data
.replace(/\x1b\[[0-9;]*[HJKmsu?lh]/g, '') // Remove ANSI escape sequences
.replace(/\s+/g, '') // Remove whitespace
.replace(ANSI_ESCAPE_PATTERN, '') // Remove ANSI escape sequences
.replace(WHITESPACE_PATTERN, '') // Remove whitespace
.trim();
const isMeaningfulActivity = meaningfulData.length > 0;