Easy to use decorator to log the execution time of a function
npm install log-execution-time-newLog Method Execution Time DecoratorA simple TypeScript decorator for logging the execution time of methods. This package helps in measuring and logging the time taken by methods to execute, useful for performance monitoring and debugging.
To install the package, run:
``sh`
npm install log-execution-time-new
Use the decorator on a synchronous method:
`typescript
import LogExecutionTime from 'log-execution-time-new';
class ExampleClass {
@LogExecutionTime()
myMethod() {
// Simulate a time-consuming task
for (let i = 0; i < 1e6; i++) {}
}
}
const instance = new ExampleClass();
instance.myMethod();
`
`typescript`
Expected Output: 2024-07-26T14:28:00.000Z myMethod: 123ms
Use the decorator on an asynchronous method:
`typescript
import LogExecutionTime from 'log-execution-time-new';
class ExampleClass {
@LogExecutionTime()
async myAsyncMethod() {
// Simulate a time-consuming task
return new Promise(resolve => setTimeout(resolve, 1000));
}
}
const instance = new ExampleClass();
instance.myMethod();
`
`typescript`
Expected Output: 2024-07-26T14:29:00.000Z myAsyncMethod: 1000ms
1. Options: Provide options that will be used while logging the execution time:
`typescript
import LogExecutionTime from 'log-execution-time-new';
class ExampleClass {
@LogExecutionTime({ keyName: "CustomKeyMethod" })
myCustomKeyMethod() {
// Simulate a time-consuming task
for (let i = 0; i < 1e6; i++) {}
}
}
const instance = new ExampleClass();
instance.myCustomKeyMethod();
`
`typescript``
Expected Output: 2024-07-26T14:30:00.000Z CustomKeyMethod: 200ms
1. keyName: Set custom key for the logged entity.
2. disable: Conditionally disable logging.
3. showExecutionCount: Add a count for how many times a function being called.