Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created July 22, 2026 17:51
Show Gist options
  • Select an option

  • Save hctilg/20e54c0f285df8fba95ad710f39c6eb3 to your computer and use it in GitHub Desktop.

Select an option

Save hctilg/20e54c0f285df8fba95ad710f39c6eb3 to your computer and use it in GitHub Desktop.
Callback Loop Balancing in JavaScript
const cores = navigator.hardwareConcurrency || 2;
let frameCount = 0;
let last_time = performance.now();
const render = now_time => {
const delta = now_time - last_time;
frameCount++;
last_time = now_time;
const FPS = Math.round(1000 / delta);
if (FPS < 50)
if ((frameCount % cores) === 0) hard_worker();
else hard_worker();
requestAnimationFrame(render);
};
requestAnimationFrame(render);
function hard_worker() {
// code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment