Last active
January 12, 2024 11:50
-
-
Save boilemmashem/4f7e280e7599ee6ec17a680e1509a1e8 to your computer and use it in GitHub Desktop.
Time a function decorator
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
/** | |
* Decorates a function so that it console logs how long it takes to run. | |
* @example | |
* // import {timed} | |
* let functionToTime = () => {...does something} | |
* functionToTime = timed(functionToTime) | |
* functionToTime() // use as normal | |
* @param func | |
* @returns | |
*/ | |
const timed = (func: Function) => (...args: any): Function => { | |
const start: number = performance.now() | |
const ret: any = func(...args) | |
console.log( | |
`function ${func.name} took ${(performance.now() - start).toFixed(3)}ms` | |
) | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment