UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

34 lines (33 loc) 1.02 kB
class FpsManager { constructor() { this.fps = 0; this.averageFPS = 0; this.history = []; const times = []; const refreshLoop = () => { const now = performance.now(); while (times.length > 0 && times[0] <= now - 1000) { times.shift(); } times.push(now); this.fps = times.length; this.history.push(this.fps); if (this.history.length === 11) { this.history.shift(); } let sum = 0; for (let i = 0; i < this.history.length; i += 1) { sum += this.history[i]; } this.averageFPS = Math.floor(sum / 10); if (typeof window === "undefined") { global.setTimeout(refreshLoop, 16); } else { window.requestAnimationFrame(refreshLoop); } }; refreshLoop(); } } export const fpsManager = new FpsManager();