Skip to main content

Import

RateLimiter

Fixed-window rate limiter. Counts requests within discrete time windows and rejects calls that exceed the limit.

Constructor

Config options

Methods

getStats return value

Example

Await allowance before sending


SlidingWindowRateLimiter

Sliding-window variant that tracks individual request timestamps instead of a simple counter. This prevents the burst problem at window boundaries that fixed-window limiters can exhibit.

Constructor

Config options

Methods

The sliding window limiter exposes the same API as RateLimiter:

Example

Choosing a strategy

Best for simple throughput caps where occasional bursts at window edges are acceptable. Lower memory overhead.
Best when you need accurate, even distribution of requests over time. Prevents the “double burst” at window boundaries. Uses slightly more memory because it stores individual timestamps.
Both limiters are in-memory and per-process. They do not coordinate across multiple server instances or browser tabs.

Per-key rate limiting

Both classes accept an optional key parameter on every method. This lets you apply separate limits per endpoint, per user, or per any other dimension.