Created
October 15, 2014 21:53
-
-
Save WA9ACE/d51659371a345a9327bd to your computer and use it in GitHub Desktop.
Simple way to calculate the FPS when using requestAnimationFrame
This file contains 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
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