The Difference Between Web and Service Workers

The Difference Between Web and Service Workers

Ferenc Almasi β€’ 2020 November 10 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

In JavaScript, you have different kind of workers. They make it possible to use and run heavy scripts in a background thread without causing the site to freeze.

Because they are running in a thread different from the main thread, you can't directly manipulate and interact with the DOM from them. Instead, you can send data to and from a worker. To spawn a new worker, use the new keyword:

Copied to clipboard!
const worker = new Worker('worker.js');
worker.js

Service workers, on the other hand, are a type of web workers. They are useful for modifying responses from network requests. They basically act as proxies. They are used for building offline apps. You can register a service worker using the register method:

Copied to clipboard!
navigator.serviceWorker.register('sw.js');
sw.js

If you would like to learn more about service worksers and handling offline state, make sure to check out the tutorial below:

Going Offline With Service Workers
Web vs service workers in JavaScript
If you would like to see more Webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.