mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 11:33:02 +00:00
feat: print performance logs to console.
This commit is contained in:
22
src/util/timedLogger.ts
Normal file
22
src/util/timedLogger.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
function isPromise<T = unknown>(p: unknown): p is Promise<T> {
|
||||
return !!p && typeof p === 'object' && 'then' in p && 'catch' in p && 'finally' in p;
|
||||
}
|
||||
|
||||
export function timedLogger<R = unknown>(label: string, fn: () => R): R {
|
||||
console.time(label);
|
||||
|
||||
try {
|
||||
const result = fn();
|
||||
|
||||
if (isPromise(result)) {
|
||||
result.finally(() => {
|
||||
console.timeEnd(label);
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
console.timeEnd(label);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user