Skip to content

Instantly share code, notes, and snippets.

@softmarshmallow
Created June 24, 2025 08:59
Show Gist options
  • Save softmarshmallow/01bffd2553a6a0025132f91ec3b343ea to your computer and use it in GitHub Desktop.
Save softmarshmallow/01bffd2553a6a0025132f91ec3b343ea to your computer and use it in GitHub Desktop.
frame budget - performance.now.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Scheduler with performance.now()</title>
</head>
<body>
<h1>Running units of work per frame…</h1>
<p>Units processed: <span id="count">0</span></p>
<script>
const countEl = document.getElementById("count");
let totalUnits = 0;
// Simulated unit of work
function doUnitOfWork() {
let x = 0;
for (let i = 0; i < 10000; i++) x += Math.sqrt(i);
totalUnits++;
}
// Scheduler
function frameLoop() {
const frameStart = performance.now();
const frameBudget = 5; // ms to do logic before yielding
while (performance.now() - frameStart < frameBudget) {
doUnitOfWork(); // Work until time runs out
}
countEl.textContent = totalUnits;
requestAnimationFrame(frameLoop); // Present and reschedule
}
requestAnimationFrame(frameLoop);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment