Web Worker used for CPU-heavy operations
Why it matters
JavaScript has a single main thread. A synchronous CSV export that filters and serializes 50,000 rows, or a bcrypt hash in a form submit handler, blocks the main thread for 200–400ms (ISO-25010 time-behaviour). During that time the browser cannot respond to any user input, animate CSS transitions, or process other JavaScript. The UI appears frozen. Web Workers solve this by running the computation on a separate thread, leaving the main thread free to handle interactions — but only if the developer moves the work there deliberately.
Severity rationale
Low because CPU-heavy main-thread operations freeze the UI only during the computation, not continuously — but the freeze is user-visible and reproducible on every trigger.
Remediation
Move CPU-heavy operations into a Web Worker. Create a dedicated worker file and communicate via postMessage:
// src/workers/export.worker.ts
self.addEventListener('message', (e: MessageEvent) => {
const { rows } = e.data
const csv = rows.map((r: Record<string, unknown>) => Object.values(r).join(',')).join('\n')
self.postMessage({ csv })
})
// src/components/ExportButton.tsx
const worker = new Worker(new URL('../workers/export.worker.ts', import.meta.url))
worker.postMessage({ rows: largeDataset })
worker.onmessage = (e) => downloadFile(e.data.csv)
Always include a main-thread fallback for environments where Worker is unavailable:
const processData = typeof Worker !== 'undefined'
? () => useWorker(data)
: () => processOnMainThread(data) // SSR or worker-restricted environments
For simpler APIs, use Comlink to expose worker functions as async callables without manual message passing.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Performance Core Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- iso-25010:2011 · performance-efficiency.time-behaviour — Time Behaviour
Taxons
History
- 2026-04-18·v1.0.0·Initial import from performance-core·automated