Created
October 5, 2023 12:34
-
-
Save sagarpanchal/ba9f67a20a1f44812ac87bb949acbae7 to your computer and use it in GitHub Desktop.
Ts log-time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Callback<T> = (...args: any[]) => T | |
export function logTime<FR>(func: Callback<FR>, name = "anonymous"): FR { | |
const t0 = performance.now() | |
// const logToConsole = process.env.JEST_WORKER_ID === undefined | |
try { | |
const output = func?.() | |
if (output instanceof Promise) { | |
output.then((output) => { | |
const t1 = performance.now() - t0 | |
console.info(`[DONE] ${func.name || name} took ${t1}`) | |
return output | |
}) | |
} | |
const t1 = performance.now() - t0 | |
console.info(`[DONE] ${func.name || name} took ${t1}`) | |
return output | |
} catch (error) { | |
const t1 = performance.now() - t0 | |
console.info(`[FAILED] ${func.name || name} took ${t1}`) | |
throw error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment