Skip to content

Instantly share code, notes, and snippets.

@Rob-ot
Created March 21, 2011 00:58
Show Gist options
  • Select an option

  • Save Rob-ot/878857 to your computer and use it in GitHub Desktop.

Select an option

Save Rob-ot/878857 to your computer and use it in GitHub Desktop.
var updateAvgFps = (function (fpsElm) {
var fpsMap = [0,0,0,0,0,0,0,0,0,0],
avgFps = 0,
lastTime = new Date().getTime(),
drawCount = 0;
if (! fpsElm) {
fpsElm = document.createElement("div");
fpsElm.style.position = "absolute";
fpsElm.style.right = "5px";
fpsElm.style.top = "5px";
document.body.appendChild(fpsElm);
}
return function () {
var now = new Date().getTime(),
delay = Math.floor(now - lastTime);
fpsMap.push(delay);
avgFps += delay;
avgFps -= fpsMap.shift();
lastTime = now;
drawCount++;
if(drawCount > 5) {
fpsElm.innerHTML = Math.round(1000 / (avgFps / 10));
drawCount = 0;
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment