Skip to main content
rule CX Codex

js-request-idle-callback — Claude Code rule: what it does

js-request-idle-callback — a rule that moves analytics, state saving and prefetching into idle browser time so the interface stops stuttering on every click.

MIT ★ 1,216 updated 2026-10-24

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add fcakyon/claude-codex-settings
/plugin install react-skills@claude-settings
Open on GitHub

What js-request-idle-callback does

The rule tells the agent to hand non-urgent work to requestIdleCallback() and keep the main thread for input and animation. The search handler is the worked example: results reach state right away, while tracking, recent-search storage and prefetching move into idle callbacks.

What you get is a set of TypeScript before/after pairs. One covers a timeout passed as the second argument, so a task still runs when the browser never idles. Another chunks a long array using deadline.timeRemaining() and re-schedules itself until the items run out. A setTimeout shim is included for engines that lack the API.

That shim matters: call window.requestIdleCallback unguarded and the code fails wherever the method is missing. Time-sensitive operations stay synchronous, and so does anything the user is waiting to see.

What it helps with

  • pull analytics out of a click handler so typing stays smooth
  • chunk a long array with deadline.timeRemaining()
  • make a tracking call fire even when the browser stays busy
  • fall back to setTimeout where requestIdleCallback is missing
  • sort work that can wait from work the user is watching

Example prompts for the agent

  • Rework handleSearch so results render immediately and analytics.track plus the recent-searches write go through requestIdleCallback.
  • Go through this file with the js-request-idle-callback rule and list what can be deferred to idle time.
  • Add a setTimeout fallback for browsers without requestIdleCallback.

How to install js-request-idle-callback

1Plugin marketplace

/plugin marketplace add fcakyon/claude-codex-settings
/plugin install react-skills@claude-settings

2Manually: put the files in place

.claude/rules/js-request-idle-callback.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about js-request-idle-callback

How do I install js-request-idle-callback in Claude Code?

From the marketplace: run /plugin marketplace add fcakyon/claude-codex-settings, then /plugin install react-skills@claude-settings. The rule itself sits at .claude/rules/js-request-idle-callback.md.

What does js-request-idle-callback do?

It shows the agent how to defer non-critical work such as analytics, state saving and prefetching to requestIdleCallback(), keeping the main thread free while the user interacts.

Which work should not be deferred?

Feedback from a user action, rendering the user is waiting on, and time-sensitive operations. The rule keeps those synchronous.

Source text

js-request-idle-callback.md · MIT
---
title: Defer Non-Critical Work with requestIdleCallback
impact: MEDIUM
impactDescription: keeps UI responsive during background tasks
tags: javascript, performance, idle, scheduling, analytics
---

## Defer Non-Critical Work with requestIdleCallback

**Impact: MEDIUM (keeps UI responsive during background tasks)**

Use `requestIdleCallback()` to schedule non-critical work during browser idle periods. This keeps the main thread free for user interactions and animations, reducing jank and improving perceived performance.

**Incorrect (blocks main thread during user interaction):**

```typescript
function handleSearch(query: string) {
  const results = searchItems(query)
  setResults(results)

  // These block the main thread immediately
  analytics.track('search', { query })
  saveToRecentSearches(query)
  prefetchTopResults(results.slice(0, 3))
}
```

**Correct (defers non-critical work to idle time):**

```typescript
function handleSearch(query: string) {
  const results = searchItems(query)
  setResults(results)

  // Defer non-critical work to idle periods
  requestIdleCallback(() => {
    analytics.track('search', { query })
  })

  requestIdleCallback(() => {
    saveToRecentSearches(query)
  })

  requestIdleCallback(() => {
    prefetchTopResults(results.slice(0, 3))
  })
}
```

**With timeout for required work:**

```typescript
// Ensure analytics fires within 2 seconds even if browser stays busy
requestIdleCallback(
  () => analytics.track('page_view', { path: location.pathname }),
  { timeout: 2000 }
)
```

**Chunking large tasks:**

```typescript
function processLargeDataset(items: Item[]) {
  let index = 0

  function processChunk(deadline: IdleDeadline) {
    // Process items while we have idle time (aim for <50ms chunks)
    while (index < items.length && deadline.timeRemaining() > 0) {
      processItem(items[index])
      index++
    }

    // Schedule next chunk if more items remain
    if (index < items.length) {
      requestIdleCallback(processChunk)
    }
  }

  requestIdleCallback(processChunk)
}
```

**With fallback for unsupported browsers:**

```typescript
const scheduleIdleWork = window.requestIdleCallback ?? ((cb: () => void) => setTimeout(cb, 1))

scheduleIdleWork(() => {
  // Non-critical work
})
```

**When to use:**

- Analytics and telemetry
- Saving state to localStorage/IndexedDB
- Prefetching resources for likely next actions
- Processing non-urgent data transformations
- Lazy initialization of non-critical features

**When NOT to use:**

- User-initiated actions that need immediate feedback
- Rendering updates the user is waiting for
- Time-sensitive operations

© fcakyon · MIT · fcakyon/claude-codex-settings

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set113

The whole set →
rendering-resource-hints The rendering-resource-hints rule tells the agent when to call React DOM's resource-hint functions inside React components. It targets server components, letting the browser start fetching assets before the client even has the HTML. CX Codex +1 ★ 1,216repo stars animation-gesture-detector-press animation-gesture-detector-press is a React Native rule that keeps press animations on the UI thread, for buttons that scale or fade while pressed. CX Codex +1 ★ 1,216repo stars commit-staged commit-staged automates commit creation: checks staged diffs, runs /simplify on code edits, and writes conventional commits using session rationale. CX Codex +1 ★ 1,216repo stars shell-and-workflows The shell-and-workflows rule documents Docker commands for the project and explains where to run frontend and backend tests. Use it when managing services or checking a change. CX Codex +1 ★ 1,216repo stars pr-comment-resolver pr-comment-resolver inspects unresolved pull request reviews, applies necessary code updates, and drafts replies for reviewers. Ideal for clearing PR feedback without manual issue hunting. CX Codex ★ 1,216repo stars list-performance-item-types list-performance-item-types is a Claude Code rule for lists with mixed layouts. It guides the agent to assign item types and keep unlike cells in separate recycling pools. CX Codex +1 ★ 1,216repo stars manage-instances manage-instances — a rule for running multiple isolated app stacks in separate worktrees, each with its own database, Redis index, and host ports. CX Codex +1 ★ 1,216repo stars test-audit The test-audit skill reviews new and changed tests, and helps spot duplicates or low-value checks. Use it when adding tests or auditing an existing suite. CX Codex +1 ★ 1,216repo stars

Project sponsors

Companies that keep this analytics open