Skip to content

Instantly share code, notes, and snippets.

@sanjeevsubedi
Created March 7, 2023 15:22
Show Gist options
  • Save sanjeevsubedi/7126e4b8bfb1a9f2aca1a7057e87c361 to your computer and use it in GitHub Desktop.
Save sanjeevsubedi/7126e4b8bfb1a9f2aca1a7057e87c361 to your computer and use it in GitHub Desktop.
Angular method decorator to calcuate the time taken to execute a function
/**
* 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