Created
June 24, 2025 08:56
-
-
Save softmarshmallow/e70c4bea6e70c3a9288c128d8c936076 to your computer and use it in GitHub Desktop.
raf.html - measure fps with raf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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