Skip to content

Instantly share code, notes, and snippets.

@softmarshmallow
Created June 24, 2025 08:56
Show Gist options
  • Save softmarshmallow/e70c4bea6e70c3a9288c128d8c936076 to your computer and use it in GitHub Desktop.
Save softmarshmallow/e70c4bea6e70c3a9288c128d8c936076 to your computer and use it in GitHub Desktop.
raf.html - measure fps with raf
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>rAF Hz Test</title>
</head>
<body>
<h1>Estimated Refresh Rate: <span id="hz">Calculating...</span></h1>
<script>
const hzDisplay = document.getElementById("hz");
const samples = [];
let last = performance.now();
function loop(now) {
const delta = now - last;
last = now;
if (delta > 0) {
const fps = 1000 / delta;
samples.push(fps);
if (samples.length > 60) samples.shift(); // keep 60 frames
const avg = samples.reduce((a, b) => a + b, 0) / samples.length;
hzDisplay.textContent = avg.toFixed(1) + " Hz";
}
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment