use-fps
shadcn-hooks/use-fpsFreeHook
A hook to calculate the FPS
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-fps.jsonSource
1import { useEffect, useRef, useState } from 'react'2import { isBrowser } from '@/registry/lib/is-browser'34export interface UseFpsProps {5 /**6 * Calculate the FPS on every x frames.7 * @default 108 */9 every?: number10}1112export function useFps(options?: UseFpsProps) {13 const { every = 10 } = options ?? {}14 const [fps, setFps] = useState<number>(0)15 const frameRef = useRef<number | null>(null)1617 useEffect(() => {18 if (19 !isBrowser ||20 typeof performance === 'undefined' ||21 typeof requestAnimationFrame === 'undefined'22 ) {23 return24 }2526 let frameCount = 027 let lastFpsUpdate = performance.now()2829 const measureFps = () => {30 const now = performance.now()31 frameCount++3233 // Update FPS every x frames34 if (frameCount >= every) {35 const timeDiff = now - lastFpsUpdate36 // Avoid division by zero or very small numbers37 const currentFps =38 timeDiff > 0 ? Math.round((frameCount * 1000) / timeDiff) : 039 setFps(currentFps)40 frameCount = 041 lastFpsUpdate = now42 }4344 frameRef.current = requestAnimationFrame(measureFps)45 }4647 frameRef.current = requestAnimationFrame(measureFps)4849 return () => {50 if (frameRef.current) {51 cancelAnimationFrame(frameRef.current)52 }53 }54 }, [every])5556 return fps57}
What it pulls in
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
