mirror of
https://github.com/stablyai/orca.git
synced 2026-10-04 08:02:09 +00:00
* perf: select highest usage totals without full sorting * test(usage): pin first-inserted tie-break contract for highestUsageKey Document why the strict > and the NaN sort fallback are load-bearing, and cover the tie/re-set ordering the replaced stable sort guaranteed. --------- Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local> Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
19 lines
729 B
TypeScript
19 lines
729 B
TypeScript
// Replaces a stable descending sort, so ties must resolve to the first-inserted
|
|
// key — hence the strict `>` below rather than `>=`.
|
|
export function highestUsageKey(totals: ReadonlyMap<string, number>): string | null {
|
|
let bestKey: string | null = null
|
|
let bestTotal = Number.NEGATIVE_INFINITY
|
|
for (const [key, total] of totals) {
|
|
if (Number.isNaN(total)) {
|
|
// NaN makes the old comparator inconsistent; defer to it verbatim so a
|
|
// corrupt total cannot change which key the summary reports.
|
|
return [...totals].sort((left, right) => right[1] - left[1])[0]?.[0] ?? null
|
|
}
|
|
if (bestKey === null || total > bestTotal) {
|
|
bestKey = key
|
|
bestTotal = total
|
|
}
|
|
}
|
|
return bestKey
|
|
}
|