Decora
Useful ECMAScript decorator for personal purposes.
Although it's written in TypeScript, these decorators should be compatible with normal JS files as long as it supports ES7.
Will be updated indefinitely.
@benchmark
Decorator Type: Method decorator
Add benchmarking functionality to a method from a class. Benchmark result will be written to console
.
Will transform the decorated function into an asynchronous function.
class Example {
@benchmark()
logMe() {
console.log('test');
}
}
Parameters
Name | Required? | Values | Default Value | Description |
---|
metric | false | ['s', 'ms', 'ns'] | 'ms' | Metric to be used when logging function runtime |
precision | false | number, n > 1 | undefined | Number of digits after comma. Passing undefined will print an abruptly long string. |
stream | true | NodeJS.WritableStream | process.stdout | Destination for the log to be written to. Detailed Explanation |
@timeout
Decorator Type: Method decorator
Limits a function execution time to a certain time limit. Will throw an error if timeout value exceeded.
Will transform the decorated function into an asynchronous function.
class Example {
@benchmark(1000)
timeoutMe() {
setTimeoutPromise(5000);
}
}
Parameters
Name | Required? | Values | Default Value | Description |
---|
time | true | number | - | Time limit for the function |
metric | false | ['s', 'ms', 'ns'] | 'ms' | Metric to be used when logging function runtime |