Skip to content

Instantly share code, notes, and snippets.

@codeandcats
Created July 19, 2020 05:18
Show Gist options
  • Select an option

  • Save codeandcats/f51a24e5674945d9d96af78868a23799 to your computer and use it in GitHub Desktop.

Select an option

Save codeandcats/f51a24e5674945d9d96af78868a23799 to your computer and use it in GitHub Desktop.

Revisions

  1. codeandcats created this gist Jul 19, 2020.
    23 changes: 23 additions & 0 deletions fps.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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));
    }
    }