micro-throttle-js
v1.0.1
Published
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.
Maintainers
Readme
Micro Throttle JS
🧸 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.
📦 Installation
npm install --save micro-throttle-js🎲 Features
- 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
💻 Usage
- 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 100ms📗 Test Coverage
PASS 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 |
----------|---------|----------|---------|---------|-------------------📘 Contributing
Contributions, suggestions, and improvements are welcome. Feel free to open issues or pull requests.
🔒 Security & Privacy
- This package is open source and intended to provide reusable utilities for application development. It does not collect, store, transmit, sell, or share user data, and it does not include analytics, tracking, telemetry, cookies, local storage usage, backend services, or project-owned data collection mechanisms.
- For more details, including vulnerability reporting guidance and consumer security recommendations, please see the Security Policy.
❤️ Support
Like this project? Support it with a github star, it would mean a lot to me! Cheers and Happy Coding.
