fpsTracker.ts
utils/fpsTracker.ts
No strong subsystem tag
48
Lines
1299
Bytes
2
Exports
0
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 48 lines, 0 detected imports, and 2 detected exports.
Important relationships
Detected exports
FpsMetricsFpsTracker
Keywords
framedurationsfirstrendertimeaveragefpslow1pctfpslastrendertimemathprivatelengthtotaltimemssorted
Detected imports
- No import paths detected.
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
export type FpsMetrics = {
averageFps: number
low1PctFps: number
}
export class FpsTracker {
private frameDurations: number[] = []
private firstRenderTime: number | undefined
private lastRenderTime: number | undefined
record(durationMs: number): void {
const now = performance.now()
if (this.firstRenderTime === undefined) {
this.firstRenderTime = now
}
this.lastRenderTime = now
this.frameDurations.push(durationMs)
}
getMetrics(): FpsMetrics | undefined {
if (
this.frameDurations.length === 0 ||
this.firstRenderTime === undefined ||
this.lastRenderTime === undefined
) {
return undefined
}
const totalTimeMs = this.lastRenderTime - this.firstRenderTime
if (totalTimeMs <= 0) {
return undefined
}
const totalFrames = this.frameDurations.length
const averageFps = totalFrames / (totalTimeMs / 1000)
const sorted = this.frameDurations.slice().sort((a, b) => b - a)
const p99Index = Math.max(0, Math.ceil(sorted.length * 0.01) - 1)
const p99FrameTimeMs = sorted[p99Index]!
const low1PctFps = p99FrameTimeMs > 0 ? 1000 / p99FrameTimeMs : 0
return {
averageFps: Math.round(averageFps * 100) / 100,
low1PctFps: Math.round(low1PctFps * 100) / 100,
}
}
}