Created
August 11, 2022 11:03
-
-
Save remyoudemans/9a1c1e2a84003985bb1c29e2711ee738 to your computer and use it in GitHub Desktop.
async time check util to find out what's slowing down a node function. I should probably use the node debugger instead.
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
const asyncTime = async ( | |
cb: (timecheck: (msg: string) => void) => Promise<any>, | |
) => { | |
let prevTime: number; | |
let nowTime: number; | |
let prevStepName: string; | |
const timecheck = (stepName: string) => { | |
if (!prevStepName) { | |
console.log('\x1b[36m%s\x1b[0m', `first step: ${stepName}`); | |
prevTime = Date.now(); | |
prevStepName = stepName; | |
return; | |
} | |
nowTime = Date.now(); | |
console.log( | |
'\x1b[36m%s\x1b[0m', | |
`from ${prevStepName} to ${stepName}: ${nowTime - prevTime}ms`, | |
); | |
prevTime = nowTime; | |
prevStepName = stepName; | |
}; | |
return cb(timecheck); | |
}; | |
/** | |
Use this something like this: | |
const myFunction = async () => { | |
return asyncTimeCheck(async timecheck => { | |
timecheck('step 1'); | |
// something to benchmark here | |
timecheck('step 2'); | |
// something else to benchmark here | |
timecheck('step 3'); | |
// something else | |
timecheck('step 4'); | |
return x; | |
}); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment