Created
April 4, 2021 05:36
-
-
Save kidGodzilla/85787f70beb3e51cd38b10c0be4a807b to your computer and use it in GitHub Desktop.
RAF Performance Check (Updated)
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
/** | |
* 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