Skip to content

Instantly share code, notes, and snippets.

@dmitryshimkin
Last active November 13, 2015 10:43
Show Gist options
  • Select an option

  • Save dmitryshimkin/8a37bea1794228528fd2 to your computer and use it in GitHub Desktop.

Select an option

Save dmitryshimkin/8a37bea1794228528fd2 to your computer and use it in GitHub Desktop.
(function () {
var start;
var isRunning = false;
var frames = 0;
var intervalId;
var entries = [];
function onFrame () {
if (isRunning) {
window.requestAnimationFrame(onFrame);
frames = frames + 1;
}
}
window.FPS = {
getFPS: function () {
var totalTime = (Date.now() - start) / 1000;
return frames / totalTime;
},
printResults: function () {
entries.forEach(function (entry) {
if (entry < 59) {
console.warn(entry);
} else {
console.info(entry);
}
});
},
reset: function () {
start = Date.now();
frames = 0;
},
start: function () {
start = Date.now();
isRunning = true;
window.requestAnimationFrame(onFrame);
},
stop: function () {
start = null;
isRunning = false;
},
startTrack: function () {
this.reset();
this.start();
intervalId = setInterval(function () {
entries.push(this.getFPS());
this.reset();
}.bind(this), 1000);
},
stopTrack: function () {
this.stop();
clearInterval(intervalId);
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment