Skip to content

Instantly share code, notes, and snippets.

@arn4v
Last active April 30, 2023 13:13
Show Gist options
  • Save arn4v/cb175c397c98a3547ab1c33a9eef00fa to your computer and use it in GitHub Desktop.
Save arn4v/cb175c397c98a3547ab1c33a9eef00fa to your computer and use it in GitHub Desktop.
type AnyPromiseFunction = (...params: any[]) => Promise<any>;
export function withPerformanceMonitoring<
T extends AnyPromiseFunction,
ReturnAwaited = Awaited<ReturnType<T>>
>(name: string, func: T): T {
const wrapped = async (...params: Parameters<T>): Promise<ReturnAwaited> => {
const timerName = `Loader performance: ${name}`;
console.time(timerName);
const data: ReturnAwaited = await func(...params);
console.timeEnd(timerName);
return data;
};
return wrapped as T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment