feat: print performance logs to console.

This commit is contained in:
鲁树人
2023-05-21 17:58:54 +01:00
parent c4e3999546
commit 4cfc672646
12 changed files with 171 additions and 92 deletions

22
src/util/timedLogger.ts Normal file
View 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;
}
}