← Concepts

阅读中文版本

Web Worker Offloading

Web worker offloading moves CPU-heavy work off the main thread into a worker, so the UI stays responsive while the computation runs in parallel and posts the result back.

browser

Definition

Web worker offloading is the practice of moving long-running or CPU-intensive computation — image processing, parsing, crypto — into a Web Worker, which runs on a separate thread, so that the main thread remains free to handle input and animation and the result is posted back when ready.

Context and scope

Applied in heavy client-side computation, file processing, and any browser task that would block the main thread longer than a frame budget.

Common misconceptions

  • Workers are just threads — they are threads without shared mutable memory, communicating by message passing; the model is different and safer
  • Offloading always helps — it helps when the work is long enough to amortize the message-passing overhead; for short work, the overhead exceeds the benefit

Anti-patterns

  • Offloading a 5ms computation to a worker, where the postMessage round-trip costs more than the computation
  • Sharing a transferable buffer and then reading it on the main thread after transfer, which is a use-after-transfer bug

Related concepts

Authoritative source

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API

What it is

Web worker offloading is the practice of moving long-running or CPU-intensive computation — image processing, parsing, crypto — into a Web Worker, which runs on a separate thread, so that the main thread remains free to handle input and animation and the result is posted back when ready. Applied in heavy client-side computation, file processing, and any browser task that would block the main thread longer than a frame budget. The browser is a hostile, shared, single-threaded (for JS) environment where every millisecond of main-thread work is a millisecond the user cannot interact.

How it works

The rendering pipeline — style, layout, paint, composite — is incremental and can be invalidated by any DOM change. Performance work targets avoiding the expensive phases (layout, paint) and scheduling the rest in idle callbacks or off the main thread. Loading work orders resources by their impact on the critical path: render-blocking scripts are deferred, images get fetch priority, and the server pushes what the client will need next.

When to apply and pitfalls

Apply when user interaction latency or load performance has a measured business impact. The pitfall is micro-optimizing a function while the page ships a megabyte of render-blocking JavaScript — the dominant cost is structural, not algorithmic. Another is measuring in the ideal lab profile and missing the long-tail devices and networks where most users actually live.