# Optimize interaction to next paint > Page responds to user interactions within 200ms, ensuring good responsiveness. **Priority:** high · **Difficulty:** advanced · **Time:** 30 min --- Interaction to Next Paint measures responsiveness throughout the user session. ## Code Example ```javascript // Bad: One long synchronous task function processLargeArray(items) { items.forEach(item => { // Heavy processing blocks main thread expensiveOperation(item) }) } // Good: Yield to main thread periodically async function processLargeArrayAsync(items) { for (let i = 0; i < items.length; i++) { expensiveOperation(items[i]) // Yield every 50ms to allow interactions if (i % 100 === 0) { await yieldToMain() } } } function yieldToMain() { return new Promise(resolve => { setTimeout(resolve, 0) }) } // Even better: Use scheduler API async function processWithScheduler(items) { for (const item of items) { expensiveOperation(item) // Yield if there's pending user input if ('scheduler' in window) { await scheduler.yield() } } } ``` ## Why It Matters INP replaced FID as a Core Web Vital in 2024—it measures how quickly your page responds to user interactions throughout the session, not just the first interaction. ## INP Score Thresholds | Score | Rating | User Perception | |-------|--------|-----------------| | 0–200ms | Good | Instant feedback | | 200–500ms | Needs improvement | Noticeable delay | | > 500ms | Poor | Sluggish, frustrating | ## INP vs FID | Metric | Measures | Replaced | |--------|----------|----------| | INP | All interactions throughout session | No | | FID | First interaction only | Yes (by INP) | ## Common INP Issues | Issue | Impact | Solution | |-------|--------|----------| | Long tasks (>50ms) | High | Break into chunks | | Heavy JavaScript | High | Code split, web workers | | Complex event handlers | Medium | Debounce, optimize | | Layout thrashing | Medium | Batch DOM reads/writes | | Third-party scripts | Medium | Lazy load, defer | ## Use Web Workers for Heavy Computation ```typescript // worker.ts self.onmessage = (e: MessageEvent) => { const { data, type } = e.data if (type === 'process') { // Heavy computation happens off main thread const result = heavyComputation(data) self.postMessage({ type: 'result', data: result }) } } function heavyComputation(data: any[]) { // Complex processing that would block main thread return data.map(item => expensiveTransform(item)) } ``` ```tsx // React component using web worker function DataProcessor({ data }) { const workerRef = useRef { workerRef.current = new Worker( new URL('./worker.ts', import.meta.url) ) workerRef.current.onmessage = (e) => { if (e.data.type === 'result') { setResult(e.data.data) } } return () => workerRef.current?.terminate() }, []) useEffect(() => { if (data && workerRef.current) { workerRef.current.postMessage({ type: 'process', data }) } }, [data]) return