Easily track and log execution time of your methods in Node and Browser apps
npm install execution-time-tracker-decorator
Measure execution time of your methods both in Node.js and Browser apps.
In terminal, run:
``sh`
npm i execution-time-tracker-decorator
In your project, import the decorators you need :
`typescript`
import { ExecTimeAsync, ExecTimeSync } from 'execution-time-tracker-decorator';
Two decorators are available:
- @ExecTimeSync() for synchronous methods@ExecTimeAsync()
- for asynchronous ones.
Examples:
`typescript
class Demo {
@ExecTimeSync()
syncFunction(): number {
let a = 0;
for (let i = 0; i < 100; i++) {
a++;
}
return a;
}
@ExecTimeAsync()
async asyncFunction(): Promise
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 300);
});
}
}
`
Both @ExecTimeSync() and @ExecTimeAsync() accept an optional object parameter which contain options to adapt to your needs. If one or more of these options are not provided, default values will be used.
: string (default = ), the title string to be logged.
- shouldLogArguments: boolean (default = false), when true, arguments passed to the decorated method will be added to the logs.
- loggerMethod: any (default = console.log), the custom logger method to use (i.e. this.logger.info if you use a custom logger).$3
`typescript
class Demo { @ExecTimeSync({ title: 'CustomTitle' })
syncFunctionA(): number {
let a = 0;
for (let i = 0; i < 10000000; i++) {
a++;
}
return a;
}
@ExecTimeSync({ shouldLogArguments: true })
syncFunctionB(param1: number, param2: string): number {
let a = param;
for (let i = 0; i < 100; i++) {
a++;
}
return a;
}
@ExecTimeSync()
syncFunctionThrow(): number {
let a = 0;
for (let i = 0; i < 10000000; i++) {
a++;
}
throw a;
}
@ExecTimeAsync({ loggerMethod: this.logger.info })
async asyncFunction(): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('bar');
}, 300);
});
}
}
`$3
`console
syncFunctionA()
CustomTitle - 8ms - Success, {
title: 'CustomTitle',
executionTime: 8,
unit: 'ms',
succeed: true,
arguments: undefined
}syncFunctionB(5, 'stringParam')
Demo::syncFunctionB - 1ms - Success, {
title: 'Demo::syncFunctionB',
executionTime: 1,
unit: 'ms',
succeed: true,
arguments: [5, 'stringParam']
}
syncFunctionThrow()
Demo::syncFunctionThrow - 8ms - Failure, {
title: 'Demo::syncFunctionThrow',
executionTime: 8,
unit: 'ms',
succeed: false,
arguments: undefined
}
`$3
When using a custom logger, be sure that the method you pass accepts multiple parameters: a main string message, and any object.
You need to pass directly the method you want to be used, i.e. this.logger.info or this.logger.debug.$3
Using a custom logger or not, the first parameter that will be passed is the main message (), the second is an object containing these properties:
`typescript
{
title: string,
executionTime: number,
unit: 'ms',
succeed: boolean,
arguments: any[] | undefined,
}
`Notes
If decorators are used in a Node.js app,
process.hrtime.bigint() will be used, resulting in a nanosecond precision execution time value, which will be expressed as milliseconds, i.e. 104136211ns will be logged as 104.1362ms.Otherwise,
new Date().valueOf()` will be used, which has a millisecond precision.