Created
March 7, 2023 15:22
-
-
Save sanjeevsubedi/7126e4b8bfb1a9f2aca1a7057e87c361 to your computer and use it in GitHub Desktop.
Angular method decorator to calcuate the time taken to execute a function
This file contains hidden or 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
/** | |
* Angular method decorator to caculate the time taken to execute a function | |
* | |
*/ | |
export function timer( | |
prototype: any, | |
name: string, | |
descriptor: PropertyDescriptor | |
) { | |
const orig = descriptor.value; | |
descriptor.value = function (...args: []) { | |
//start the timer | |
const start = performance.now(); | |
// run the function | |
orig.apply(this, args); | |
// stop the timer | |
const stop = performance.now(); | |
console.log(`Total time taken:`, stop - start); | |
}; | |
} | |
/** | |
* How to use it? | |
* | |
* @timer | |
* getData() { | |
* ...... | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment