Skip to content

perf: cache p50 in LatencyTracker instead of allocating+sorting per request #322

@kianwoon

Description

@kianwoon

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions