Skip to content

🧸 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.

License

Notifications You must be signed in to change notification settings

NPM-Workbench/micro-throttle-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

dam-banner-1 npm downloads license NPM Unpacked Size

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

  1. Typescript support, preserving this context
  2. Works with any function signature
  3. Zero dependencies, tiny footprint
  4. Modern ESM support
  5. First function call executes immediately, followed by delay based subsequent calls

πŸ’» Usage

  1. 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)
  1. 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.

❀️ Support

Like this project? Support it with a github star, it would mean a lot to me! Cheers and Happy Coding.

About

🧸 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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors