Defined in: rate-limiter.ts:156
A class that creates a rate-limited function.
Rate limiting is a simple approach that allows a function to execute up to a limit within a time window, then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where all executions happen immediately, followed by a complete block. This synchronous version is lighter weight and often all you need - upgrade to AsyncRateLimiter when you need promises, retry support, abort capabilities, or advanced error handling.
The rate limiter supports two types of windows:
For smoother execution patterns, consider using:
Rate limiting is best used for hard API limits or resource constraints. For UI updates or smoothing out frequent events, throttling or debouncing usually provide better user experience.
State Management:
const rateLimiter = new RateLimiter(
(id: string) => api.getData(id),
{
limit: 5,
window: 1000,
windowType: 'sliding',
}
);
// Will execute immediately until limit reached, then block
rateLimiter.maybeExecute('123');
const rateLimiter = new RateLimiter(
(id: string) => api.getData(id),
{
limit: 5,
window: 1000,
windowType: 'sliding',
}
);
// Will execute immediately until limit reached, then block
rateLimiter.maybeExecute('123');
TFn extends AnyFunction
new RateLimiter<TFn>(fn, initialOptions): RateLimiter<TFn>;
new RateLimiter<TFn>(fn, initialOptions): RateLimiter<TFn>;
Defined in: rate-limiter.ts:163
TFn
RateLimiterOptions<TFn>
RateLimiter<TFn>
fn: TFn;
fn: TFn;
Defined in: rate-limiter.ts:164
key: string | undefined;
key: string | undefined;
Defined in: rate-limiter.ts:159
options: RateLimiterOptions<TFn>;
options: RateLimiterOptions<TFn>;
Defined in: rate-limiter.ts:160
readonly store: Store<Readonly<RateLimiterState>>;
readonly store: Store<Readonly<RateLimiterState>>;
Defined in: rate-limiter.ts:157
getMsUntilNextWindow(): number;
getMsUntilNextWindow(): number;
Defined in: rate-limiter.ts:356
Returns the number of milliseconds until the next execution will be possible
number
getRemainingInWindow(): number;
getRemainingInWindow(): number;
Defined in: rate-limiter.ts:348
Returns the number of remaining executions allowed in the current window
number
maybeExecute(...args): boolean;
maybeExecute(...args): boolean;
Defined in: rate-limiter.ts:250
Attempts to execute the rate-limited function if within the configured limits. Will reject execution if the number of calls in the current window exceeds the limit.
...Parameters<TFn>
boolean
const rateLimiter = new RateLimiter(fn, { limit: 5, window: 1000 });
// First 5 calls will return true
rateLimiter.maybeExecute('arg1', 'arg2'); // true
// Additional calls within the window will return false
rateLimiter.maybeExecute('arg1', 'arg2'); // false
const rateLimiter = new RateLimiter(fn, { limit: 5, window: 1000 });
// First 5 calls will return true
rateLimiter.maybeExecute('arg1', 'arg2'); // true
// Additional calls within the window will return false
rateLimiter.maybeExecute('arg1', 'arg2'); // false
reset(): void;
reset(): void;
Defined in: rate-limiter.ts:367
Resets the rate limiter state
void
setOptions(newOptions): void;
setOptions(newOptions): void;
Defined in: rate-limiter.ts:189
Updates the rate limiter options
Partial<RateLimiterOptions<TFn>>
void
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.