Skip to content

Commit 7407ad3

Browse files
committed
Fix NaN handling in ttftBucketIndex
The function didn't validate that ttftMs is a finite number. If ttftMs was NaN or Infinity, Math.max(NaN, 1) would return NaN, causing Math.log(NaN) to return NaN, and the entire calculation would produce NaN. Added Number.isFinite() check to default to 0 for invalid numbers. The existing test suite (11 tests, 995 assertions) already covers edge cases including NaN, Infinity, and negative values, and all pass with this fix.
1 parent 7b65652 commit 7407ad3

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

common/src/util/ttft-histogram.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const LN_BASE = Math.log(TTFT_HISTOGRAM_BASE)
3737
* Sub-millisecond and zero samples land in bucket 0 rather than at -Infinity.
3838
*/
3939
export function ttftBucketIndex(ttftMs: number): number {
40-
const index = Math.floor(Math.log(Math.max(ttftMs, 1)) / LN_BASE)
40+
const safeTtftMs = Number.isFinite(ttftMs) ? ttftMs : 0
41+
const index = Math.floor(Math.log(Math.max(safeTtftMs, 1)) / LN_BASE)
4142
return Math.min(TTFT_HISTOGRAM_BUCKET_COUNT - 1, Math.max(0, index))
4243
}
4344

0 commit comments

Comments
 (0)