Skip to content

Instantly share code, notes, and snippets.

@preco21
Last active June 8, 2016 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preco21/8af61ea67f849ab91231fc6883f41781 to your computer and use it in GitHub Desktop.
Save preco21/8af61ea67f849ab91231fc6883f41781 to your computer and use it in GitHub Desktop.
A simple fps counter
import getTimeMethod from './getTimeMethod';
class FPSCounter {
constructor() {
this._getTime = getTimeMethod();
this._fps = 0;
this._prevTime = 0;
this._frames = 0;
this.startTick();
}
startTick() {
this._prevTime = this._getTime();
}
tick() {
const currentTime = this._getTime();
this._frames++;
if (currentTime - this._prevTime > 1000) {
this._fps = Math.round(this._frames * 1000 / (currentTime - this._prevTime));
this._prevTime = currentTime;
this._frames = 0;
}
}
getFPS() {
return this._fps;
}
}
export {
FPSCounter as default,
};
function getTimeMethod() {
return window.performance && window.performance.now
? () => window.performance.now()
: () => Date.now();
}
export {
getTimeMethod as default,
};
@preco21
Copy link
Author

preco21 commented May 25, 2016

const counter = new FPSCounter();

function update() {
  requestAnimationFrame(b);
  counter.tick();
  console.log(counter.getFPS());
}

update();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment