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
/plugin marketplace add fcakyon/claude-codex-settings
/plugin install react-skills@claude-settingsWhat 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
setTimeoutwhererequestIdleCallbackis missing - sort work that can wait from work the user is watching
Example prompts for the agent
- Rework
handleSearchso results render immediately andanalytics.trackplus the recent-searches write go throughrequestIdleCallback. - Go through this file with the
js-request-idle-callbackrule and list what can be deferred to idle time. - Add a
setTimeoutfallback for browsers withoutrequestIdleCallback.
How to install js-request-idle-callback
1Plugin marketplace
/plugin marketplace add fcakyon/claude-codex-settings/plugin install react-skills@claude-settings2Manually: put the files in place
.claude/rules/js-request-idle-callback.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-request-idle-callback.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-request-idle-callback.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-request-idle-callback.mdManually: put the files in place
.clinerules/js-request-idle-callback.mdStep 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