UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

131 lines (115 loc) 4.4 kB
/* eslint-disable no-console */ /** * Represents a Timer with functionalities for measuring and tracking time. */ class Timer { startTime: number; totalTime: number = 0; callCount: number = 0; /** * Constructs a new Timer object and sets the start time as passed. * @param {number} startTime - The start time in milliseconds. */ constructor(startTime: number) { this.startTime = startTime; } /** * Checks if timer is accumulative, i.e., more than one timing instance has been recorded. * @returns {boolean} - Returns true if more than one instance is recorded. */ isAccumulative(): boolean { return this.callCount > 1; } /** * Adds the elapsed time from the start time to the total time and increments call count. * @param {number} currentTime - The current time in milliseconds. * @param {number} [precision=3] - The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive. * @returns {string} - The elapsed time in milliseconds. */ addElapsedTime(currentTime: number, precision: number = 3): string { const elapsed = currentTime - this.startTime; this.totalTime += elapsed; this.callCount++; return elapsed.toFixed(precision); } /** * Resets the timer properties back to their initial state. */ clearTimer() { this.startTime = 0; this.totalTime = 0; this.callCount = 0; } /** * Returns the total time recorded by the timer in string format using toFixed(). * @param {number} [precision=3] - The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive. * @returns {string} - The total time in milliseconds. */ getTotalTime(precision: number = 3): string { return this.totalTime.toFixed(precision); } /** * Returns the average time by dividing total time with the call count. * @param {number} [precision=3] - The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive. * @returns {string} - The average time in milliseconds. */ getAverageTime(precision: number = 3): string { return (this.totalTime / this.callCount).toFixed(precision); } } // Exporting an object that maps labels to their timer instances. export const timers: { [label: string]: Timer } = {}; /** * Starts or resets the timer for a given label. * @param {string} label - The label associated with the timer. * @example * time('apiCall'); */ export function time(label: string) { const currentTime = performance.now(); if (timers[label]) { timers[label].startTime = currentTime; } else { timers[label] = new Timer(currentTime); } } /** * Ends the timer for a given label and logs the time elapsed for the timer. * The operation can be accumulative, i.e., add the elapsed time to a total, and can also log intermediate time. * @param {string} label - The label associated with the timer. * @param {Object} settings - Settings for logging and accumulation. * @param {boolean} settings.accumulative - If true, adds the elapsed time to a total (accumulates time). * @param {boolean} settings.intermediateLog - If true, logs the intermediate elapsed time. * @example * timeEnd('apiCall', { accumulative: true, intermediateLog: true }); */ export function timeEnd( label: string, settings?: { accumulative?: boolean; intermediateLog?: boolean } ) { const currentTime = performance.now(); if (timers[label]) { if (timers[label].isAccumulative() || settings?.accumulative) { if (!settings?.accumulative) { const totalTime = timers[label].getTotalTime(); const callCount = timers[label].callCount; const averageTime = timers[label].getAverageTime(); console.log( `${label} - Total: ${totalTime}ms, Calls: ${callCount}, Average: ${averageTime}ms` ); timers[label].clearTimer(); } else { const elapsed = timers[label].addElapsedTime(currentTime); if (settings?.intermediateLog) { console.log(`${label}: ${elapsed}ms 1`, { label, settings }); } } } else { const elapsed = timers[label].addElapsedTime(currentTime); console.log(`${label}: ${elapsed}ms 2`, { label, settings }); timers[label].clearTimer(); } } else { console.log(`${label}: Timer not found`); } }