Skip to content

rate-limiter-utils is an npm package designed to optimize how frontend applications interact with APIs. It helps manage API request rates by implementing debouncing and throttling techniques, which improve performance and prevent issues like exceeding API rate limits.

License

Notifications You must be signed in to change notification settings

anuragkmr45/rate-limiter-utils

Repository files navigation

rate-limiter-utils

rate-limiter-utils is an npm package designed to optimize how frontend applications interact with APIs. It helps manage API request rates by implementing debouncing and throttling techniques, which improve performance and prevent issues like exceeding API rate limits.

✨ Features

  • Debouncing: Delays the execution of a function until a specific time has passed since its last call. Ideal for reducing unnecessary API calls triggered by frequent events, such as user input in a search field.

  • Throttling: Limits how often a function can be called within a set timeframe. Useful for handling high-frequency actions like scrolling or mouse movements.

By using rate-limiter-utils, you can:

  • 🚀 Boost application performance by reducing unnecessary API requests.

  • ⚡ Prevent API rate limits from being reached and improve the overall user experience.

  • 💡 Optimize resource usage by managing network traffic and CPU load more efficiently.

🛠️ Installation

You can install rate-limiter-utils via npm:

npm install rate-limiter-utils

📚 Usage

Here's how you can use the debounce and throttle functions in your frontend application:

Debouncing Example

  • Standard debounce (function executes after delay):

import { debounce } from 'rate-limiter-utils';

const debouncedFunction = debounce(() => console.log('Executed!'), 300);

debouncedFunction();  // Will log "Executed!" after 300ms
  • Immediate execution (executes immediately and then debounced):

If immediate is true, the function is executed at the start and then debounced. If false, it behaves like a standard debounce function.
import { debounce } from 'rate-limiter-utils';

const debouncedFunction = debounce(() => console.log('Executed!'), 300, true);

debouncedFunction();  // Will log "Executed!" after 300ms

Throttling Example

  • Standard throttle (function is invoked every delay ms at most):

import { debounce } from 'rate-limiter-utils';

const throttledFunction = throttle(() => console.log('Throttled function!'), 300);

throttledFunction();  // Will log "Throttled function!" if enough time has passed since the last call
  • Immediate execution (function is executed immediately, and then throttled):

If immediate is true, the function is executed at the start and then throttled. If false, it behaves like a standard throttling function.
import { debounce } from 'rate-limiter-utils';

const throttledImmediateFunction = throttle(() => console.log('Throttled function!'), 300, true);

throttledImmediateFunction();  // Will log immediately on the first call, and throttle subsequent calls

🛠️ Technologies

  • TypeScript: Ensures type safety and better developer experience.
  • Node.js: Runtime for building the package.

📝 License

This project is licensed under the MIT License.

👤 Author

Developed by Anurag Kumar.

About

rate-limiter-utils is an npm package designed to optimize how frontend applications interact with APIs. It helps manage API request rates by implementing debouncing and throttling techniques, which improve performance and prevent issues like exceeding API rate limits.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published