Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - What can service workers do that web workers cannot?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...