log-execution-time-new
v2.10.0
Published
Easy to use decorator to log the execution time of a function
Downloads
55
Maintainers
Readme
Log Method Execution Time Decorator
A 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.
Installation
To install the package, run:
npm install log-execution-time-new
Basic Example
Use the decorator on a synchronous method:
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();
Expected Output: 2024-07-26T14:28:00.000Z myMethod: 123ms
Use the decorator on an asynchronous method:
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();
Expected Output: 2024-07-26T14:29:00.000Z myAsyncMethod: 1000ms
Additional Parameters
- Options: Provide options that will be used while logging the execution time:
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();
Expected Output: 2024-07-26T14:30:00.000Z CustomKeyMethod: 200ms
Available Options
- keyName: Set custom key for the logged entity.
- disable: Conditionally disable logging.
- showExecutionCount: Add a count for how many times a function being called.
Contributing
Feel free to submit issues or pull requests if you have suggestions for improvements or find bugs.
License
This package is licensed under the ISC License