Can anyone help me implement a timing decorator for my Typescript classes similar to what is commonly used with Python?
Consider the following example class
class MyClass() {
private _foo: string = 'abc';
public printX() : void {
console.log('x');
}
public add(a: number, b:number) : number {
return a + b;
}
public get foo() : string {
return this._foo;
}
}
I want to decorate my class like so
@TimeMethods()
class MyClass() { .... }
In order to record the execution time of all functions. Specifically, I only want the functions printX
and add(a,b)
to be timed, while disregarding variables and getters _foo
and get foo
.
I have already created a decorator to time individual functions
export function TimeDebug(): MethodDecorator {
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
const start = new Date();
originalMethod.apply(this, args);
console.log(`Execution of ${key.toString()} took ${new Date().getTime() - start.getTime()} ms.`);
};
return descriptor;
};
}
How can I automatically apply this decorator to each function within a class that has been decorated as such?