Skip to content

Instantly share code, notes, and snippets.

@0xlkda
Created May 13, 2024 08:34
Show Gist options
  • Save 0xlkda/965f8aa5ee1c98beb64acef6742408d0 to your computer and use it in GitHub Desktop.
Save 0xlkda/965f8aa5ee1c98beb64acef6742408d0 to your computer and use it in GitHub Desktop.
log function args and output with execution time
import { UnitOfTime } from './converter.js'
function timing(fn) {
let perf = performance
let A = perf.now()
let result = fn()
let B = perf.now()
return [result, B - A]
}
async function timingAsync(asyncFn) {
let perf = performance
let A = perf.now()
let result = await asyncFn()
let B = perf.now()
return [result, B - A]
}
function track(fn, logger = console) {
return function () {
logger.debug(`input:`, arguments)
let [result, ms] = timing(() => fn.apply(fn, arguments))
logger.debug(`output:`, result, UnitOfTime.Miliseconds(ms).seconds.toFixed(5), 'seconds')
return result
}
}
function trackAsync(asyncFn, logger) {
return async function () {
logger.debug(`input:`, arguments)
let [result, ms] = await timingAsync(() => asyncFn.apply(asyncFn, arguments))
logger.debug(`output:`, result, UnitOfTime.Miliseconds(ms).seconds.toFixed(5), 'seconds')
return result
}
}
export {
track,
trackAsync,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment