Skip to content

Instantly share code, notes, and snippets.

@codeandcats
Created July 19, 2020 05:18
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 codeandcats/f51a24e5674945d9d96af78868a23799 to your computer and use it in GitHub Desktop.
Save codeandcats/f51a24e5674945d9d96af78868a23799 to your computer and use it in GitHub Desktop.
FPS Counter (Frames Per Second)
export class FpsCounter {
constructor(private maxSamples: number = 100) {}
private samples: number[] = [];
sample() {
this.samples.push(Date.now());
if (this.samples.length > this.maxSamples) {
this.samples.shift();
}
return this.count();
}
count() {
if (this.samples.length < 2) {
return 0;
}
const start = this.samples[0];
const end = this.samples[this.samples.length - 1];
return this.samples.length * (1000 / (end - start));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment