Created
July 22, 2026 17:51
-
-
Save hctilg/20e54c0f285df8fba95ad710f39c6eb3 to your computer and use it in GitHub Desktop.
Callback Loop Balancing in JavaScript
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
| 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