Choose and implement browser workers
Web Worker or Service Worker for a Calculation or Network Request?
Decide which browser worker fits a calculation or request interception, using page ownership, messaging and lifecycle as the checks.

If a page starts a CPU-intensive calculation and needs the result back, choose a dedicated Web Worker. If the requirement is to intercept a request from a page the worker controls, choose a Service Worker. Both run outside the page’s main JavaScript thread, but that shared property is not the deciding factor. Ask who initiates the work and how the result must reach the page.
Start with the requirement, not the worker name
Suppose a browser app has two jobs. A user asks it to calculate a large result while the page remains available for interaction. Separately, the app needs to handle a particular resource request before it reaches the network. These are distinct requirements, even though both can involve work outside the main thread.
For the calculation, identify the initiating script: it is the page script responding to the user. A dedicated worker is used by a single script, and workers exchange data with the main thread through postMessage() and message events. The page sends input, receives the result and updates its own DOM. Moving the calculation out of the main thread is useful here because intensive work need not occupy the thread responsible for the page’s interactions. MDN’s Web Workers API guide describes the dedicated-worker relationship and message exchange.
For the resource request, ask a different question: must browser request handling be intercepted for a page associated with the worker? A Service Worker is registered against an origin and path and can control associated pages, intercepting navigation and resource requests. That is the capability this requirement calls for. A calculation worker returning a message to its page does not, by doing so, become a request interceptor.
Use ownership and lifecycle to rule out the wrong worker
The dedicated worker’s relationship is with the script using it. That makes it a straightforward choice when one page script supplies calculation input and awaits an answer by message. The data in those messages is copied rather than shared, so do not plan the page-worker boundary as though both scripts were editing the same message data. Neither worker can directly manipulate the page’s DOM; the page must make any visible update after it receives information.
A Service Worker has a different relationship with the app. Its lifecycle is separate from the webpage’s, and it can stop when unused and restart when needed. In-memory state therefore cannot be assumed to survive between uses. If an interception rule depends on state that must survive a restart, it needs persistent storage rather than an ordinary worker variable. More importantly for this choice, being off the main thread does not make a Service Worker an always-running calculation process for a page. This lifecycle comparison states the stop-and-restart constraint explicitly.
Check capabilities and communication before choosing
Trace the intended path in each case. For computation, it is page script → dedicated worker → message back to page script → DOM update. The worker does not need to own a browser request. For interception, it is request from a controlled page → Service Worker request handling. The page can display an eventual result, but registering the worker is not itself evidence that a particular request was intercepted. First establish that the requesting page is controlled, then check what the request handling actually produced.
Service Workers are available only in secure contexts. MDN’s Service Worker API guide specifies HTTPS and notes that browsers treat http://localhost as secure for local development. That constraint matters before attempting registration; it says nothing by itself about whether a subsequent request was intercepted.
Apply both choices in one app
In the example app, assign the large calculation to a dedicated worker because the page initiates it and needs a result by message. Assign the resource-request requirement to a Service Worker because the desired action is interception of a request from a page it controls. These incomplete page-side calls show where each choice begins, not working computation or proof of interception:
const calculator = new Worker('/calculation-worker.js');
navigator.serviceWorker.register('/service-worker.js');
The first call still needs worker code and page-side message handling. The second still needs Service Worker request handling, page control and a request to observe. In particular, a successful registration answers only whether registration succeeded. It cannot tell you that the request you care about reached the Service Worker’s request handling. Check that request’s handling separately. If the page needs to show either the calculation result or the request outcome, its own script makes the DOM change. This keeps the choice tied to the application requirement instead of treating two kinds of worker as interchangeable background processes.
