useFps
React Sensor Hooks that tracks FPS (frames per second)
useFps measures the current frames per second by counting requestAnimationFrame callbacks. It returns a number representing the current FPS, calculated over a configurable number of frames (default: every 10 frames). This is useful for performance monitoring and adaptive rendering.
When to Use
- Displaying a real-time FPS counter in development or debugging overlays
- Adaptively reducing animation complexity when frame rate drops below a threshold
- Benchmarking rendering performance of components or pages
Notes
- SSR-safe: Returns
0during server-side rendering. NorequestAnimationFramecalls occur on the server. - Performance: The hook uses
requestAnimationFrameinternally, so it has minimal overhead. Adjust theeveryoption to balance accuracy and update frequency. - Related hooks: See also
useIdlefor detecting user inactivity, which can complement FPS-based adaptive rendering.
Usage
Live Editor
function Demo() { const fps = useFps(); return <div>FPS: {fps}</div>; };
Result
API
useFps
Returns
number: frames per second
Arguments
| Argument | Description | Type | DefaultValue |
|---|---|---|---|
| options | - | UseFpsOptions | undefined | - |
UseFpsOptions
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| every | Calculate the FPS on every x frames. | number | 10 |