Skip to content

Instantly share code, notes, and snippets.

@WA9ACE
Created October 15, 2014 21:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WA9ACE/d51659371a345a9327bd to your computer and use it in GitHub Desktop.
Save WA9ACE/d51659371a345a9327bd to your computer and use it in GitHub Desktop.
Simple way to calculate the FPS when using requestAnimationFrame
window.onload = function() {
var lastCalledTime;
var counter = 0;
var fpsArray = [];
function update(timestamp) {
var fps;
if (!lastCalledTime) {
lastCalledTime = new Date().getTime();
fps = 0;
}
var delta = (new Date().getTime() - lastCalledTime) / 1000;
lastCalledTime = new Date().getTime();
fps = Math.ceil((1/delta));
if (counter >= 60) {
var sum = fpsArray.reduce(function(a,b) { return a + b });
var average = Math.ceil(sum / fpsArray.length);
console.log(average);
counter = 0;
} else {
if (fps !== Infinity) {
fpsArray.push(fps);
}
counter++;
}
window.requestAnimationFrame(update);
}
window.requestAnimationFrame(update);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment