Created
July 19, 2020 05:18
-
-
Save codeandcats/f51a24e5674945d9d96af78868a23799 to your computer and use it in GitHub Desktop.
FPS Counter (Frames Per Second)
This file contains hidden or 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
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