skip to content

Workers, Service Workers, and Offline

You will learn how to get work off the main thread and how a service worker turns a site into an offline-capable, installable app. Interviewers ask because both answer the same recurring question: what do you do when one thread is not enough?

on this pageshow

explore

questions

page 2 of 2

Your team wants a service worker to cache authenticated API responses in Cache Storage so the app works offline. How would you decide whether that is acceptable, and what would you require before approving it?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Weigh the offline benefit against storing per-user data on disk under an origin-wide, unencrypted namespace keyed by URL. Approve it only for read data whose staleness is harmless, with per-user cache names, a hard clear on logout and account switch, and a bounded, audited entry set.

open as a page

You are designing a browser worker pool that processes large binary frames. How do you decide between transferring ArrayBuffers back and forth and putting the data in a SharedArrayBuffer that all the workers read?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Prefer transfer: it is zero-copy, needs no special headers, and gives exclusive ownership so no synchronization is possible to get wrong. Choose SharedArrayBuffer only when several agents must read or write the same bytes concurrently — and accept cross-origin isolation plus atomics as the price.

open as a page

You are designing a browser feature that runs CPU-heavy work in dedicated Web Workers. How do you decide how many workers to create and whether they are long-lived or spawned per task, and what does `navigator.hardwareConcurrency` actually tell you?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

Size a small pool, typically hardwareConcurrency minus one and capped low, and keep it long-lived rather than spawning per task. hardwareConcurrency reports logical processors visible to the page, not free capacity — user agents may clamp it, and the main thread and other tabs are competing.

open as a page

showing 31–33 of 33