Skip to main content
Web DevPerformance & OptimizationDiagramIntermediate

Web Worker

KO: 웹 워커 (Web Worker)·EN: Web Worker·JA: Web Worker(ウェブワーカー)

A browser API that runs heavy JavaScript computations on a separate background thread so the UI main thread stays responsive at 60fps

Table of Contents

Structure Diagram

Main Thread Execution vs Web Worker Background Offload
Main ThreadMain Thread Only

UI & Heavy Compute Compete

  • Animation & ScrollFreezes during heavy loops
  • INP ResponsivenessClicks delayed by Long Tasks
  • DOM AccessDirect DOM access
Web WorkerDedicated Web Worker

Separate Thread · postMessage

  • Animation & ScrollSmooth 60fps UI while computing
  • INP ResponsivenessMain thread stays free for input
  • DOM AccessNo DOM (communicate via messages)
Quick Answer

What is Web Worker?

A browser API that runs heavy JavaScript computations on a separate background thread so the UI main thread stays responsive at 60fps. It is a core concept in Performance & Optimization within Web Dev.

Core Principles

Because browser JavaScript shares a single main thread with layout, painting, and user input, CPU-heavy work like large JSON parsing, image processing, or encryption freezes the interface.

A Web Worker spawns an isolated background thread that runs JavaScript in parallel. Workers have no direct access to window or the DOM—avoiding shared-memory race conditions—and exchange data with the main thread via postMessage / onmessage.

Practical Takeaways

  • Avoid serialization bottlenecks with Transferable Objects: Cloning multi-megabyte ArrayBuffer payloads can negate worker gains. Pass [buffer] as the transfer list for zero-copy ownership transfer.
  • Offload genuinely heavy tasks: Spawning a worker and marshaling messages has overhead. Target tasks exceeding ~50ms such as compression, syntax parsing, cryptography, or WebAssembly execution.
  • Simplify messaging with RPC wrappers: Libraries like Comlink let you call worker methods with normal async/await syntax instead of hand-writing message switchboards.

Notable Examples

  • In-browser image/video editors and PDF tools: Run filters, OffscreenCanvas rendering, and Wasm encoders in workers so sliders and previews stay at 60fps.
  • Web IDEs (VS Code for the Web / Monaco): Execute language servers, TypeScript type-checking, and full-text search indexing off the main thread.