Skip to content

Instantly share code, notes, and snippets.

@boilemmashem
Last active January 12, 2024 11:50
Show Gist options
  • Save boilemmashem/4f7e280e7599ee6ec17a680e1509a1e8 to your computer and use it in GitHub Desktop.
Save boilemmashem/4f7e280e7599ee6ec17a680e1509a1e8 to your computer and use it in GitHub Desktop.
Time a function decorator
/**
* 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