Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ticker {
- private ticksPerSecond: { [id: string]: number; }
- private lastTick: { [id: string]: number; }
- constructor() {
- this.ticksPerSecond = {};
- this.lastTick = {};
- }
- private now() {
- return performance.now() / 1000;
- }
- public tick(id: string) {
- const smoothing = 0.99;
- const now = this.now();
- if (!(id in this.ticksPerSecond)) {
- this.ticksPerSecond[id] = 0;
- }
- if (id in this.lastTick) {
- const tps = 1 / (now - this.lastTick[id]);
- this.ticksPerSecond[id] = this.ticksPerSecond[id] * smoothing + (tps * (1 - smoothing));
- }
- this.lastTick[id] = now;
- }
- public dump() {
- for (let id in this.ticksPerSecond) {
- console.log(id + ": " + this.ticksPerSecond[id]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement