mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-05-11 18:09:50 +08:00
add measureExecutionTime
This commit is contained in:
21
packages/ui/src/common/utils/performance.ts
Normal file
21
packages/ui/src/common/utils/performance.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export const measureExecutionTime = async <
|
||||
T extends Promise<unknown> | ((...args: unknown[]) => unknown),
|
||||
U = T extends Promise<unknown>
|
||||
? T
|
||||
: T extends (...args: unknown[]) => unknown
|
||||
? ReturnType<T>
|
||||
: never
|
||||
>(
|
||||
promiseOrFunction: T
|
||||
): Promise<U> => {
|
||||
const startTime = new Date()
|
||||
if (promiseOrFunction instanceof Promise) {
|
||||
await promiseOrFunction
|
||||
console.log(`execution duration ${Number(new Date()) - Number(startTime)}ms`)
|
||||
return promiseOrFunction as U
|
||||
} else {
|
||||
const result = await promiseOrFunction()
|
||||
console.log(`execution duration ${Number(new Date()) - Number(startTime)}ms`)
|
||||
return result as U
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user