Shadcn Hooks

useFps

A hook to calculate the FPS

Loading...

Installation

npx shadcn@latest add @hooks/use-fps
pnpm dlx shadcn@latest add @hooks/use-fps
yarn dlx shadcn@latest add @hooks/use-fps
bun x shadcn@latest add @hooks/use-fps

Copy and paste the following code into your project.

use-fps.ts
import { useEffect, useRef, useState } from 'react'
import { isBrowser } from '@/registry/lib/is-browser'

export interface UseFpsProps {
  /**
   * Calculate the FPS on every x frames.
   * @default 10
   */
  every?: number
}

export function useFps(options?: UseFpsProps) {
  const { every = 10 } = options ?? {}
  const [fps, setFps] = useState<number>(0)
  const frameRef = useRef<number | null>(null)

  useEffect(() => {
    if (
      !isBrowser ||
      typeof performance === 'undefined' ||
      typeof requestAnimationFrame === 'undefined'
    ) {
      return
    }

    let frameCount = 0
    let lastFpsUpdate = performance.now()

    const measureFps = () => {
      const now = performance.now()
      frameCount++

      // Update FPS every x frames
      if (frameCount >= every) {
        const timeDiff = now - lastFpsUpdate
        // Avoid division by zero or very small numbers
        const currentFps =
          timeDiff > 0 ? Math.round((frameCount * 1000) / timeDiff) : 0
        setFps(currentFps)
        frameCount = 0
        lastFpsUpdate = now
      }

      frameRef.current = requestAnimationFrame(measureFps)
    }

    frameRef.current = requestAnimationFrame(measureFps)

    return () => {
      if (frameRef.current) {
        cancelAnimationFrame(frameRef.current)
      }
    }
  }, [every])

  return fps
}

API

interface UseFpsProps {
  /**
   * Calculate the FPS on every x frames.
   * @default 10
   */
  every?: number
}

/**
 * A hook to calculate the FPS
 * @param options - The options for the FPS
 * @param options.every - The number of frames to calculate the FPS on
 * @default { every: 10 }
 * @returns The FPS
 */
export function useFps(options?: UseFpsProps): number

Credits

Last updated on