Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Created April 4, 2021 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidGodzilla/85787f70beb3e51cd38b10c0be4a807b to your computer and use it in GitHub Desktop.
Save kidGodzilla/85787f70beb3e51cd38b10c0be4a807b to your computer and use it in GitHub Desktop.
RAF Performance Check (Updated)
/**
* RAF Performance Checking
*
* Will enable and disable a flag on the window object
* When Javascript performance suffers, so that optional features
* Can be Disabled or delayed
*/
(function () {
var lastTimestamp = + new Date();
/**
* Compares the execution of requestAnimationFrame to it's ideal, 60fps.
* When Javascript is performant, the difference should approach 16ms.
* When experiencing degraded Javascript performance, this value will begin to increase.
* If it reaches 32, we toggle the flag `window.performant`, which other
* functions can use to turn off the execution of optional features
*/
function perfCheck () {
var difference = + new Date() - lastTimestamp;
lastTimestamp = + new Date();
window.performant = difference < 32;
requestAnimationFrame(perfCheck);
}
requestAnimationFrame(perfCheck);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment