@jointly/performance-logger
v0.2.4
Published
This is a library providing both a higher-order function and a decorator that can be used to measure the performance of a function. It can be used both with `sync` and `async` functions.
Downloads
10
Keywords
Readme
What is it?
This is a library providing both a higher-order function and a decorator that can be used to measure the performance of a function.
It can be used both with sync
and async
functions.
How do I install it?
You can install it by using the following command:
npm install @jointly/performance-logger
How do I use it?
Both the higher-order function and the decorator can be used to measure the performance of a function.
The decorator can be used to measure the performance of a class method, while the higher-order function can be used to measure the performance of any function.
Higher-Order Function
First, you need to create a logger.
You can do this by using the logPerformance
function.
Then, you can wrap the function you want to measure by using the logger.
import { logPerformance } from '@jointly/performance-logger';
const mockFn = (a, b) => a + b;
const logger = logPerformance({
name: 'my-logger',
validation: true,
log: console.log
});
const loggedMockFn = logger(mockFn);
loggedMockFn(1, 2);
loggedMockFn(2, 3);
Example output (using console.log
):
{
name: 'my-logger',
executionTime: 0.08937501907348633,
args: [ 1, 2 ],
result: 3
}
{
name: 'my-logger',
executionTime: 0.011166989803314209,
args: [ 2, 3 ],
result: 5
}
Decorator
import { LogPerformance } from '@jointly/performance-logger';
class MyClass {
@LogPerformance({
name: 'my-logger',
validation: true,
log: console.log
})
myMethod(a, b) {
return a + b;
}
}
const myClass = new MyClass();
myClass.myMethod(1, 2);
myClass.myMethod(2, 3);
Example output (using console.log
):
{
name: 'my-logger',
executionTime: 0.07745802402496338,
args: [ 1, 2 ],
result: 3
}
{
name: 'my-logger',
executionTime: 0.012000024318695068,
args: [ 2, 3 ],
result: 5
}
Tests
You can run the tests by using the following command:
npm test
How does it work?
The decorator will measure the time it takes for a function to execute and log it to the console / the given logger.
It expects an object with the following properties:
name
(required for higher-order function, optional for decorator): The name of the function to be measuredlog
(optional): The function to call with the measuration results. Defaults toconsole.log
. It will be called with the following parameters:name
: The name of the function to be measuredexecutionTime
: The time it took for the function to execute in millisecondsargs
: The arguments passed to the function to be measuredresult
: The result of the function to be measured
validation
(optional): A function or a boolean that will be called / verified before the function to be measured is called. Defaults totrue
. If it returnsfalse
, the function will be called but the measuration will not be logged.- If it is a function, it will be called with the arguments passed to the function to be measured.
- If it is a boolean, it will be used as the validation result.
Other Info
The name
parameter
The name
parameter is used to identify the function/method to be measured.
Please, be aware that even if the LogPerformance decorator can generate a name if not passed (based on class name and property name), it could lead to unexpected labels in an environment where the code is minified, obfuscated or transpiled.
For this reason, it is highly recommended to pass a name to the decorator.