Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference

createRateLimiter

Function: createRateLimiter()

ts
function createRateLimiter<TFn>(fn, initialOptions): SolidRateLimiter<TFn>
function createRateLimiter<TFn>(fn, initialOptions): SolidRateLimiter<TFn>

Defined in: rate-limiter/createRateLimiter.ts:62

A low-level Solid hook that creates a RateLimiter instance to enforce rate limits on function execution.

This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that you can integrate with any state management solution (createSignal, etc).

Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing, it does not attempt to space out or collapse executions intelligently.

The rate limiter supports two types of windows:

  • 'fixed': A strict window that resets after the window period. All executions within the window count towards the limit, and the window resets completely after the period.
  • 'sliding': A rolling window that allows executions as old ones expire. This provides a more consistent rate of execution over time.

For smoother execution patterns:

  • Use throttling when you want consistent spacing between executions (e.g. UI updates)
  • Use debouncing when you want to collapse rapid-fire events (e.g. search input)
  • Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)

Type Parameters

TFn extends AnyFunction

Parameters

fn

TFn

initialOptions

RateLimiterOptions<TFn>

Returns

SolidRateLimiter<TFn>

Example

tsx
// Basic rate limiting - max 5 calls per minute with a sliding window
const rateLimiter = createRateLimiter(apiCall, {
  limit: 5,
  window: 60000,
  windowType: 'sliding',
  onReject: (rateLimiter) => {
    console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
  }
});

// Access rate limiter state via signals
console.log('Executions:', rateLimiter.executionCount());
console.log('Rejections:', rateLimiter.rejectionCount());
console.log('Remaining:', rateLimiter.remainingInWindow());
console.log('Next window in:', rateLimiter.msUntilNextWindow());
// Basic rate limiting - max 5 calls per minute with a sliding window
const rateLimiter = createRateLimiter(apiCall, {
  limit: 5,
  window: 60000,
  windowType: 'sliding',
  onReject: (rateLimiter) => {
    console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
  }
});

// Access rate limiter state via signals
console.log('Executions:', rateLimiter.executionCount());
console.log('Rejections:', rateLimiter.rejectionCount());
console.log('Remaining:', rateLimiter.remainingInWindow());
console.log('Next window in:', rateLimiter.msUntilNextWindow());
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.