Adopt a performance-safety standard: prevent unbounded state growth in monitoring, and keep critical-path CPU/memory work linear and avoidable.
Apply these rules: 1) Treat performance instrumentation as state with lifecycle
2) Keep streaming/decoding parsers linear
data event.3) Precompute repeated work in bounded loops
4) Add cheap guards before expensive transforms
5) Gate unnecessary upstream calls
Example patterns (self-contained):
// 1) Bounded performance marks performance.mark(“omni-request-body-size”, { detail: bodySize }); // … read/observe in your monitoring pipeline … performance.clearMarks(“omni-request-body-size”);
// 2) Rolling buffer (linear-time scanning) const chunks: Buffer[] = []; let processed = 0; req.on(“data”, (chunk) => { chunks.push(Buffer.from(chunk)); const buffer = Buffer.concat(chunks); // process from processed.. and advance processed cursor // then drop processed prefix to keep future work bounded });
// 3) Precomputed lookup const unsupportedParamsByModel = new Map<string, readonly string[]>(); // module init fills map function getUnsupportedParams(modelId: string) { return unsupportedParamsByModel.get(modelId) ?? []; }
// 4) Cheap guard before expensive work if (payload.length > MAX_BYTES * 2) throw new Error(“too large”); // only then do regex/strip/decode
// 5) Latency gate if (!hasAnyEnforcementCondition) return; // skip upstream preflight