type TimeLoggerParams = { label?: string disabled?: boolean } // For debugging & profiling purposes export class TimeLogger { private label: string private disabled: boolean | undefined private startTimestamp: ReturnType | undefined protected getTimeInMs() { if (!this.startTimestamp) { return 0 } return (performance.now() - this.startTimestamp).toFixed(2) } public constructor(params?: TimeLoggerParams) { const label = params?.label ?? `TimeLogger[${TimeLogger.getRandomLabel()}]` this.label = label this.disabled = params?.disabled ?? false } public static getRandomLabel = () => (Math.random() + 1).toString(36).substring(7) public startTime() { this.startTimestamp = performance.now() } public setDisabled(disabled: boolean) { this.disabled = disabled } public log(...params: Parameters) { if (!this.disabled) { let timestamp = '' if (this.startTimestamp) { timestamp = ` ${this.getTimeInMs()}ms -` } console.log(`${this.label}`, timestamp, ...params) } } }