import { Extremes , FpsAnalyzrSettings } from "./types" ; /**
 * Default maximum number of FPS measurements to keep.
 */ export const defaultMaximumKept = 250 ; /**
 * Storage and analysis for framerate measurements.
 */ export class FpsAnalyzr { /**
     * How many FPS measurements to keep at any given time, at most.
     */ private readonly maximumKept : number ; /**
     * History of FPS measurements.
     */ private readonly measurements : number [ ] ; /**
     * How many timestamp deltas have been measured.
     */ private recordedMeasurements : number ; /**
     * How many ticks have been received.
     */ private recordedTicks : number ; /**
     * The current position in the internal measurements listing.
     */ private ticker : number ; /**
     * The most recent timestamp received.
     */ private timeCurrent : number ; param settings public constructor ( settings = { } ) { this . maximumKept = settings . maximumKept === undefined ? defaultMaximumKept : settings . maximumKept ; this . recordedMeasurements = 0 ; this . recordedTicks = 0 ; this . ticker = 0 ; this . measurements = [ ] ; } param time public tick = ( time ) : void => { if ( this . timeCurrent !== undefined ) { this . measurements [ this . ticker ] = 1000 / ( time - this . timeCurrent ) ; this . recordedMeasurements += 1 ; this . ticker = ( this . ticker += 1 ) % this . maximumKept ; } this . timeCurrent = time ; this . recordedTicks += 1 ; } ; returns public getAverage ( ) { if ( this . measurements . length === 0 ) { return 0 ; } const realRecordedLength = Math . min ( this . maximumKept , this . recordedMeasurements ) ; let total = 0 ; for ( let i = 0 ; i < realRecordedLength ; i += 1 ) { total += this . measurements [ i ] ; } return total / realRecordedLength ; } returns public getExtremes ( ) { if ( this . measurements . length === 0 ) { return { highest : 0 , lowest : 0 , } ; } const realRecordedLength = Math . min ( this . maximumKept , this . recordedMeasurements ) ; let lowest = this . measurements [ 0 ] ; let highest = lowest ; for ( let i = 1 ; i < realRecordedLength ; i += 1 ) { const fps = this . measurements [ i ] ; if ( fps > highest ) { highest = fps ; } else if ( fps < lowest ) { lowest = fps ; } } return { highest , lowest } ; } returns public getMedian ( ) { if ( this . measurements . length === 0 ) { return 0 ; } const realRecordedLength = Math . min ( this . maximumKept , this . recordedMeasurements ) ; const copy = this . measurements . slice ( 0 , realRecordedLength ) . sort ( ) ; const fpsKeptHalf = Math . floor ( realRecordedLength / 2 ) ; return copy . length % 2 === 0 ? ( copy [ fpsKeptHalf - 1 ] + copy [ fpsKeptHalf ] ) / 2 : copy [ fpsKeptHalf ] ; } returns public getRecordedTicks ( ) { return this . recordedTicks ; } }