CodePark/ questions / Web workers & service workers
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › Web workers & service workers

What is the difference between a web worker and a service worker?

A web worker moves heavy work off the main thread for one page. A service worker sits between your pages and the network, like a programmable proxy.

What they share:

  • Both run JavaScript on a separate thread.
  • Neither can access the DOM.
  • Both talk to pages with postMessage.

A web worker (also called a dedicated worker) is created by a page with new Worker("worker.js"). It lives as long as that page. Its job is to move CPU-heavy work off the main thread, so the UI stays responsive. Examples are parsing large files, processing images or running complex calculations.

A service worker is registered for an origin and a scope (a URL path). It acts as a programmable network proxy between the pages and the network. It can:

  • intercept fetch requests
  • serve responses from the Cache API
  • handle push notifications and background sync

A service worker is event-driven. It runs independently of any single page, and the browser can stop it when it is idle. It requires HTTPS, except on localhost.

  • Both run on a separate thread, have no DOM access, and use postMessage.
  • A web worker moves heavy computation off the main thread for one page.
  • A service worker is a network proxy for a whole origin.
  • A service worker enables offline support, caching and push notifications.
  • Service workers need HTTPS.