Created
May 13, 2024 08:34
-
-
Save 0xlkda/965f8aa5ee1c98beb64acef6742408d0 to your computer and use it in GitHub Desktop.
log function args and output with execution 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
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