Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/js-request-idle-callback.md
1---2title: Defer Non-Critical Work with requestIdleCallback3impact: MEDIUM4impactDescription: keeps UI responsive during background tasks5tags: javascript, performance, idle, scheduling, analytics6---78## Defer Non-Critical Work with requestIdleCallback910**Impact: MEDIUM (keeps UI responsive during background tasks)**1112Use `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.1314**Incorrect (blocks main thread during user interaction):**1516```typescript17function handleSearch(query: string) {18const results = searchItems(query)19setResults(results)2021// These block the main thread immediately22analytics.track('search', { query })23saveToRecentSearches(query)24prefetchTopResults(results.slice(0, 3))25}26```2728**Correct (defers non-critical work to idle time):**2930```typescript31function handleSearch(query: string) {32const results = searchItems(query)33setResults(results)3435// Defer non-critical work to idle periods36requestIdleCallback(() => {37analytics.track('search', { query })38})3940requestIdleCallback(() => {41saveToRecentSearches(query)42})4344requestIdleCallback(() => {45prefetchTopResults(results.slice(0, 3))46})47}48```4950**With timeout for required work:**5152```typescript53// Ensure analytics fires within 2 seconds even if browser stays busy54requestIdleCallback(55() => analytics.track('page_view', { path: location.pathname }),56{ timeout: 2000 }57)58```5960**Chunking large tasks:**6162```typescript63function processLargeDataset(items: Item[]) {64let index = 06566function processChunk(deadline: IdleDeadline) {67// Process items while we have idle time (aim for <50ms chunks)68while (index < items.length && deadline.timeRemaining() > 0) {69processItem(items[index])70index++71}7273// Schedule next chunk if more items remain74if (index < items.length) {75requestIdleCallback(processChunk)76}77}7879requestIdleCallback(processChunk)80}81```8283**With fallback for unsupported browsers:**8485```typescript86const scheduleIdleWork = window.requestIdleCallback ?? ((cb: () => void) => setTimeout(cb, 1))8788scheduleIdleWork(() => {89// Non-critical work90})91```9293**When to use:**9495- Analytics and telemetry96- Saving state to localStorage/IndexedDB97- Prefetching resources for likely next actions98- Processing non-urgent data transformations99- Lazy initialization of non-critical features100101**When NOT to use:**102103- User-initiated actions that need immediate feedback104- Rendering updates the user is waiting for105- Time-sensitive operations106