← Back to list

Webworker and Serviceworker

Webworker and its uses

Mansithakur · 2022-08-19 09:51 · 7 claps · 1.3 min read
#javascript #service-worker #webworker
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Webworker and Serviceworker

Webworker and its uses

Have you ever wondered if you have closed the tab from where you are downloading something, even then downloading doesn’t stop. The script which is working won’t exist if the tab is closed. So how does it works?

Since JavaScript is single-threaded, it will use a single thread to perform tasks like rendering a page, doing heavy calculations and all other tasks. It lacks the capabilities to perform multithreading. So for this purpose, it could be achieved on the web by using workers to run scripts in background threads, allowing them to perform tasks without interfering with the main thread. Workers are an entire JavaScript scope running on a separate thread, without any shared memory.

But there is a catch, they don’t have access to document and window objects, thus they can’t interact with DOM directly as they run in the secondary thread without blocking the main thread and user interface.

So what is the difference between a webworker and a serviceworker?

Service worker allows one to intercept network requests — via the fetch API, or to listen to push notifications via the push API.

Another important thing is a page can have several web workers, but the service worker sees all tabs under the domain it is registered with.

A web worker is attached to the tab, so if the tab closes then it will get terminated.

Serviceworker exists even when the tab is open.

Use case:

  • A web worker is usually related to heavy load work to avoid heavy load UI.
  • ​​Service workers' tasks are generally more related to acting as a network proxy, handling background tasks, and things like caching and offline.

Hope it helps to bring you some understanding.


메타데이터
post_id
3be9ed0f29d5
slug
webworker-and-serviceworker-3be9ed0f29d5
url
https://medium.com/@adhyaya/webworker-and-serviceworker-3be9ed0f29d5
canonical_url
https://medium.com/@adhyaya/webworker-and-serviceworker-3be9ed0f29d5
author_url
https://medium.com/@adhyaya
status
ok
fetched_at
2026-06-29 22:44:20