What is decko?
Decko is a collection of decorators for JavaScript/TypeScript that simplify common patterns such as binding methods to their instances, memoizing function results, and throttling or debouncing function calls.
What are decko's main functionalities?
Bind
The @bind decorator ensures that the method is always called with the correct 'this' context, even if it's extracted from the instance.
class MyClass {
@bind
myMethod() {
console.log(this);
}
}
const instance = new MyClass();
const method = instance.myMethod;
method(); // Logs the instance of MyClass
Memoize
The @memoize decorator caches the result of a function call and returns the cached result when the same inputs occur again, avoiding redundant computations.
class MyClass {
@memoize
computeExpensiveValue(input) {
console.log('Computing...');
return input * 2;
}
}
const instance = new MyClass();
console.log(instance.computeExpensiveValue(2)); // Logs 'Computing...' and then 4
console.log(instance.computeExpensiveValue(2)); // Logs 4 without 'Computing...'
Debounce
The @debounce decorator ensures that the decorated function is only called once within the specified time frame, even if it's triggered multiple times.
class MyClass {
@debounce(300)
handleResize() {
console.log('Resized!');
}
}
const instance = new MyClass();
window.addEventListener('resize', () => instance.handleResize());
Throttle
The @throttle decorator ensures that the decorated function is called at most once within the specified time frame, even if it's triggered multiple times.
class MyClass {
@throttle(300)
handleScroll() {
console.log('Scrolled!');
}
}
const instance = new MyClass();
window.addEventListener('scroll', () => instance.handleScroll());
Other packages similar to decko
core-decorators
Core-decorators is a library that provides a set of decorators for common patterns in JavaScript/TypeScript, such as @autobind, @memoize, @debounce, and @throttle. It offers similar functionality to decko but with a broader set of decorators.
lodash-decorators
Lodash-decorators is a library that provides decorators for lodash functions, including @debounce, @throttle, and @memoize. It integrates lodash's powerful utility functions with the decorator syntax, offering a more extensive set of utilities compared to decko.
typescript-decorators
Typescript-decorators is a library that provides a collection of decorators for TypeScript, including method decorators like @autobind, @debounce, and @throttle. It is similar to decko but is specifically designed for TypeScript.
decko
A concise implementation of the three most useful decorators:
@bind
: make the value of this
constant within a method@debounce
: throttle calls to a method@memoize
: cache return values based on arguments
Decorators help simplify code by replacing the noise of common patterns with declarative annotations.
Conversely, decorators can also be overused and create obscurity.
Decko establishes 3 standard decorators that are immediately recognizable, so you can avoid creating decorators in your own codebase.
💡 Tip: decko is particularly well-suited to Preact Classful Components.
💫 Note:
Installation
Available on npm:
npm i -S decko
Usage
Each decorator method is available as a named import.
import { bind, memoize, debounce } from 'decko';
@bind
class Example {
@bind
foo() {
return this;
}
}
let e = new Example();
assert.equal(e.foo.call(null), e);
@memoize
Cache values returned from the decorated function.
Uses the first argument as a cache key.
Cache keys are always converted to strings.
Options:
caseSensitive: false
- Makes cache keys case-insensitive
cache: {}
- Presupply cache storage, for seeding or sharing entries
class Example {
@memoize
expensive(key) {
let start = Date.now();
while (Date.now()-start < 500) key++;
return key;
}
}
let e = new Example();
let one = e.expensive(1);
let two = e.expensive(1);
let three = e.expensive(2);
@debounce
Throttle calls to the decorated function. To debounce means "call this at most once per N ms".
All outward function calls get collated into a single inward call, and only the latest (most recent) arguments as passed on to the debounced function.
Options:
delay: 0
- The number of milliseconds to buffer calls for.
class Example {
@debounce
foo() {
return this;
}
}
let e = new Example();
for (let i=1000; i--) e.foo();
License
MIT