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
fetchrequests - 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.