← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Event Loop Blocking — Long Tasks

javascript ES5 Intermediate
debt(d7/e5/b5/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list Lighthouse and Chrome DevTools, both of which are specialist profiling tools that require manual invocation and interpretation — not automated linters. The code_pattern hints (.sort, .filter, .map) could theoretically be flagged, but context is needed to know whether input size makes them blocking; there is no automated signal flagged in the metadata.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix involves moving CPU-intensive work to Web Workers or worker_threads — this is not a single-line swap. It requires introducing a new worker file, message-passing wiring, and restructuring the calling code. Breaking loops into setTimeout(0) chunks is simpler but also requires non-trivial refactoring of the computation logic.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The term applies_to both web and cli contexts broadly. Any codebase doing significant data processing must continuously reason about whether computation stays within event-loop-safe bounds. It doesn't define the system's shape, but it imposes an ongoing discipline tax across many work streams whenever new data-processing features are added.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception is explicit: developers believe async/await prevents blocking because it looks like asynchronous code, but async/await only yields on I/O. CPU-bound synchronous code inside async functions still fully blocks the event loop. This directly contradicts the intuition built from using async/await for I/O, making it a serious and well-documented gotcha.

About DEBT scoring →

TL;DR

JavaScript is single-threaded — synchronous code that runs >50ms blocks the event loop, freezing UI and delaying I/O callbacks. Break long tasks into chunks.

Explanation

The browser and Node.js run JS on a single thread via an event loop. Synchronous operations hold the thread exclusively — while sorting a 100k array, no clicks, animations, or API callbacks can run. The 50ms threshold (from RAIL model) is the point beyond which users perceive delay. Solutions: setTimeout(fn, 0) or requestAnimationFrame() to yield, Web Workers for CPU tasks in browsers, Node.js worker_threads for CPU-bound work. For data processing: process in chunks with setImmediate() between batches. Identify blocking tasks with Lighthouse, Chrome Performance tab, or Long Tasks API.

Common Misconception

async/await prevents event loop blocking — async/await only yields on I/O. CPU-bound synchronous code inside async functions still blocks the loop.

Why It Matters

Blocking the event loop for >50ms makes UIs unresponsive and degrades server throughput in Node.js — a common cause of poor Lighthouse scores and slow APIs.

Common Mistakes

  • Running heavy synchronous computation (sorting, filtering large arrays) in the main thread.
  • Parsing large JSON synchronously: JSON.parse(hugeString).
  • Not using Web Workers for CPU-intensive client-side operations.

Code Examples

✗ Vulnerable
// Blocks for hundreds of ms on large arrays:
const sorted = bigArray.sort((a, b) => a.value - b.value);
const results = sorted.filter(complexPredicate);
// UI frozen during this
✓ Fixed
// Chunk-based processing with yields:
async function processInChunks(items, chunkSize = 100) {
    for (let i = 0; i < items.length; i += chunkSize) {
        const chunk = items.slice(i, i + chunkSize);
        processChunk(chunk);
        await new Promise(r => setTimeout(r, 0)); // Yield to event loop
    }
}

// Web Worker for CPU-intensive work in browsers

Added 22 Mar 2026
Edited 5 Apr 2026
Views 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 5 Unknown AI 3 ChatGPT 2 Majestic 1 Meta AI 1 Google 1 Ahrefs 1
crawler 18 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
Move CPU-intensive work to Web Workers (browser) or worker_threads (Node). Break loops into chunks with setTimeout(0) yields. Measure with Chrome Performance tab / Lighthouse.
📦 Applies To
javascript ES5 web cli
🔗 Prerequisites
🔍 Detection Hints
\.sort\(|\.filter\(|\.map\(
Auto-detectable: ✗ No lighthouse chrome-devtools
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: Function Tests: Update
CWE-400

✓ schema.org compliant