212

What can service workers do that web workers cannot? Or vice versa?

It seems that web workers are a subset of the functionality of service workers. Is this correct?

Ali
  • 56,466
  • 29
  • 168
  • 265
Ben Aston
  • 53,718
  • 65
  • 205
  • 331

3 Answers3

267

Buksy's answer is correct but in my opinion it does not answer the original question, namely: "What can service workers do that web workers cannot? Or vice versa?"

There are fundamental differences in their lifecycle and the number of instances per origin you can have. In short:

               | Web Workers  | Service Workers  |
|--------------|--------------|------------------|
| Instances    | Many per tab | One for all tabs |
| Lifespan     | Same as tab  | Independent      |
| Intended use | Parallelism  | Offline support  |

Buksy's answer is basically the last row of the table. Credit: I took this table from Demystifying Web Workers and Service Workers by Nolan Lawson, starting from slide 35.

In particular, here is how you spawn and terminate web workers:

Using Web Workers

whereas service workers have their own lifecycle, which is admittedly their "most complicated part":

The Service Worker Lifecycle

So lifecyle is one fundamental difference between the two (a consequence of their intended use).

There used to be a huge difference in browser support: Service workers were not available at all in Safari for iOS till 11.3 (2018 Mar 29), see Can I use service workers? In contrast, web workers had a much better browser support already in 2012: Can I use web workers?

If you have to support IE11, you can only use web workers: IE11 does not have service workers, and apparently the end of support for IE11 is October 14, 2025.

There are subtle differences in their API support across browsers, see HTML5 Worker Test (also by Nolan Lawson). In a particular browser, one kind of worker might support a certain API call whereas the other does not. Visit that page and test your own browser!

Ali
  • 56,466
  • 29
  • 168
  • 265
  • 4
    That table explains it very well. The original question would probably be better phrased as "What's the difference between..." – Drenai Oct 07 '19 at 16:57
  • 2
    What can service workers do that shared web workers cannot? – Pacerier Jun 07 '20 at 04:29
  • 4
    This didn't answer the question, which was what can service workers do that web workers cannot. This answer only pastes summaries of what they are intended to do, and differences in aspects like instances. There's no identification of anything that service workers can do that web workers cannot. For example, can web workers also intercept requests and load resources from cache, like service workers do? Can web workers have a longer lifespan than a tab? That is, how firm is the purported constraint on their lifespan? Is there any way to force a different lifespan? – LearningFast Apr 28 '21 at 14:28
  • This answer is informative and useful as is, but should also be self-sufficient, and isn't entirely. The middle section linking to lifecycles is useless without visiting the underlying site. – SgtPooki Mar 22 '23 at 16:53
205

There is a big difference in what they are intended for:

Web Workers

Web Workers provide a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. In addition, they can perform I/O using XMLHttpRequest (although the responseXML and channel attributes are always null). Once created, a worker can send messages to the JavaScript code that created it by posting messages to an event handler specified by that code (and vice versa.)

Source - Using Web Workers

Service Worker

Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.

Source - Service Worker API

So Web Workers are handy to run expensive scripts without causing the user interface to freeze, while Service Workers are useful to modify the response from network requests (for example, when building an offline app).

MattMS
  • 1,106
  • 1
  • 16
  • 32
Buksy
  • 11,571
  • 9
  • 62
  • 69
  • 11
    What can service workers do that web workers cannot? – Pacerier Jun 07 '20 at 04:28
  • 1
    @Pacerier read the second quotation and last paragraph to answer your question – Buksy Jun 08 '20 at 10:05
  • 5
    I agree with Pacerier that this does not answer the question. I only tells what one does and what the other. A reader might come to a conclusion based on it, but a good answer should clarify, maybe even confirm or disconfirm a readers conclusion. – kca Jun 17 '20 at 12:01
  • 1
    I like the other answer that Ali has provided, its a wider description of differences between those two techs. But I think the last paragraph of my answer clearly answers the question, the *Web Workers can help you to build the offline app*. If you think theres a better answer, feel free to add another :) – Buksy Jun 19 '20 at 09:28
  • 2
    @Buksy, A typo in your comment: the [[ Service ]] Workers can help you to build the offline app – Ahmad Mobaraki Jul 25 '20 at 10:58
  • This didn't answer the question, which was what can service workers do that web workers cannot. This answer only pastes summaries of what they do or are intended to do. There's no identification of anything that service workers can do that web workers cannot. For example, can web workers intercept requests and load resources from cache? – LearningFast Apr 28 '21 at 14:25
26

Service workers

enter image description here

Service workers are a proxy between the browser and the network. By intercepting requests made by the document, service workers can redirect requests to a cache, enabling offline access.

/* main.js */

navigator.serviceWorker.register('/service-worker.js');



/* service-worker.js */

// Install 
self.addEventListener('install', function(event) {
    // ...
});

// Activate 
self.addEventListener('activate', function(event) {
    // ...
});

// Listen for network requests from the main document
self.addEventListener('fetch', function(event) {
    // ...
});

Web workers

enter image description here

Web workers are general-purpose scripts that enable us to offload processor-intensive work from the main thread.

/* main.js */

// Create worker
const myWorker = new Worker('worker.js');

// Send message to worker
myWorker.postMessage('Hello!');

// Receive message from worker
myWorker.onmessage = function(e) {
  console.log(e.data);
}

Original Post Here

sanket naik
  • 378
  • 3
  • 7