Performance — Tier 2 (Medium Impact)
File: src/hedging.ts:43-49
Problem
getAdaptiveDelay() calls getP50() on every request when hedging is enabled. getP50() does .map(s => s.ttfbMs) (allocates a new array) then .sort() — O(n log n) per call on a hot path. With maxSize=30, this is 30-element sort per request.
Fix
Cache the p50 value in LatencyTracker — recompute only when the sliding window changes (on record()):
private p50Cache = new Map<string, number>();
record(provider: string, ttfbMs: number): void {
// ... existing logic ...
this.p50Cache.delete(provider);
}
getP50(provider: string): number {
const cached = this.p50Cache.get(provider);
if (cached !== undefined) return cached;
// compute, store in p50Cache, return
}
Impact
Eliminates array allocation + sort on every hedged request.
Source: Performance review (2026-04-25)
Performance — Tier 2 (Medium Impact)
File:
src/hedging.ts:43-49Problem
getAdaptiveDelay()callsgetP50()on every request when hedging is enabled.getP50()does.map(s => s.ttfbMs)(allocates a new array) then.sort()— O(n log n) per call on a hot path. WithmaxSize=30, this is 30-element sort per request.Fix
Cache the p50 value in
LatencyTracker— recompute only when the sliding window changes (onrecord()):Impact
Eliminates array allocation + sort on every hedged request.
Source: Performance review (2026-04-25)