π§Έ A tiny, type-safe throttle utility for modern JavaScript and TypeScript. Like a water dam regulating a powerful river β micro-throttle-js controls the flow of your function calls.
π High frequency events like scroll, resize, mousemove, rapid API triggers etc., can overwhelm your application. Just like a dam regulates water flow to prevent flooding, micro-throttle-js regulates how often your function executes.
No chaos. No overflow. Just controlled release.
npm install --save micro-throttle-js- Typescript support, preserving
thiscontext - Works with any function signature
- Zero dependencies, tiny footprint
- Modern ESM support
- First function call executes immediately, followed by delay based subsequent calls
- Basic Example
/* node modules */
import { microThrottle } from 'micro-throttle-js';
/* func */
function simpleLogger(message: string): void {
console.log(message);
}
const throttledLog = microThrottle(simpleLogger, 1000);
throttledLog('Hello'); // Executes immediately
throttledLog('World'); // Ignored (within 1000ms)- Event Listener Example
/* node modules */
import { microThrottle } from 'micro-throttle-js';
/* func */
function handleEvent(event: Event): void {
console.log(`Scrolled: ${window.scrollY}px`);
}
/* event listener */
const handleOnScroll = microThrottle(handleEvent, 100);
window.addEventListener('scroll', handleOnScroll);
// Good for performance-sensitive UI interactions.
// Here, when scrolling, the throttle package helps ensure to execute the package only every 100msPASS src/micro-throttle/test/index.test.ts
microThrottle
β throws when delay is negative
β throws when delay is not finite
β calls once immediately and throttles calls within delay
β preserves this context
β works with zero delay
β throws when delay is Infinity
β separate instances do not share throttle state
Test Suites: 1 passed, 1 total
Tests: 7 passed, 7 total
Snapshots: 0 total
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Contributions, suggestions, and improvements are welcome.
Feel free to open issues or pull requests.
Like this project? Support it with a github star, it would mean a lot to me! Cheers and Happy Coding.
