Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Created October 5, 2023 12:34
Show Gist options
  • Save sagarpanchal/ba9f67a20a1f44812ac87bb949acbae7 to your computer and use it in GitHub Desktop.
Save sagarpanchal/ba9f67a20a1f44812ac87bb949acbae7 to your computer and use it in GitHub Desktop.
Ts log-time
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