Skip to content

Instantly share code, notes, and snippets.

@stryju
Created November 30, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stryju/4694ac189925f7120b139a1c1b2b29b9 to your computer and use it in GitHub Desktop.
Save stryju/4694ac189925f7120b139a1c1b2b29b9 to your computer and use it in GitHub Desktop.
function useFPS() {
const [fps, setFPS] = useState(0);
useEffect(() => {
let then = performance.now();
let frames = 0;
let req;
(function loop(now) {
req = window.requestAnimationFrame(loop);
frames++;
if (now - then >= 1000) {
setFPS(Math.round((frames * 1000) / (now - then)));
frames = 0;
then = now;
}
})(then);
return () => window.cancelAnimationFrame(req);
}, [setFPS]);
return fps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment