Questions
20 of 20
1What is a Web Worker and why do we need it? What problem does it solve in the browser?
2What are the three types of Web Workers? How do they differ from each other?
3How do you create a Web Worker and communicate with it?
4What is the postMessage / onmessage pattern? Is communication synchronous or asynchronous?
5What APIs and features are not available inside a Web Worker? (e.g., DOM, window, document)
6What is the Structured Clone Algorithm? How is it different from JSON serialization? What types can and cannot be cloned?
7What are Transferable Objects? How does transferring an ArrayBuffer differ from cloning it, and why does it matter for performance?
8How do you handle errors inside a Web Worker? What events are available and what information do they expose?
9What is a Shared Worker? How can multiple browser tabs communicate through one? What are its limitations?
10How do you terminate a Web Worker? What's the difference between worker.terminate() and self.close()?
11How would you architect a thread pool using Web Workers to process a large task queue efficiently?
12What is SharedArrayBuffer and Atomics? How do they enable true shared memory between the main thread and workers? What security headers are required to use them?
13What are the performance trade-offs of using Web Workers? When can they actually hurt performance (e.g., small tasks, frequent postMessage calls)?
14How would you use Web Workers with WebAssembly (WASM) for CPU-intensive tasks? What makes this combination powerful?
15How do Module Workers work (type: 'module')? What advantages do they offer over classic workers?
16In a React or Vue app, how would you integrate a Web Worker cleanly without breaking the component lifecycle? Have you used libraries like comlink — how does it simplify the worker communication model?
17What is the lifecycle of a Service Worker (install → activate → fetch)? How do you handle cache versioning and old worker cleanup?
18What is the difference between cache.put(), cache.add(), and cache.addAll()? How do you implement a cache-first vs network-first strategy?
19How do you communicate between a Service Worker and the main thread? What is the role of BroadcastChannel vs postMessage here?
20Can Web Workers access localStorage?
20 / 20

Can Web Workers access localStorage?

Difficulty: 5/10
Web Workers, localStorage, thread communication

No, Web Workers cannot access localStorage or sessionStorage. They can use IndexedDB, CacheStorage, or postMessage to communicate with main thread for storage operations.

localStorage is synchronous and blocks the thread, which would defeat the purpose of workers. Use IndexedDB for asynchronous storage within workers.

Scenario Questions

0-2 years experience

  1. 1If you need to read a value from localStorage inside a Web Worker, how would you do it?
  2. 2What will happen if you call localStorage.getItem() directly from a worker script?
  3. 3Suppose a worker wants to cache intermediate results; would you store them in localStorage from the worker? Why or why not?

2-5 years experience

  1. 1You added a Web Worker to offload image processing, but the worker now throws a SecurityError when trying to use localStorage. Walk me through how you would debug and fix it.
  2. 2Explain the trade‑offs between sending data to the main thread for storage via postMessage versus trying to access localStorage directly from the worker.
  3. 3During a rollout, some browsers report 'localStorage is undefined' when the worker runs. What could cause this and how would you make the feature robust?

5-8 years experience

  1. 1Design a module that uses Web Workers for heavy computation and needs to persist state across page reloads. How would you architect the storage layer given workers can't access localStorage?
  2. 2With hundreds of workers per user session, discuss performance and security implications of using IndexedDB via the main thread versus a SharedWorker pattern for storage.
  3. 3Your team wants to migrate a legacy codebase that heavily relies on localStorage into a worker‑centric architecture. What migration steps and compatibility concerns would you address?

8+ years experience

  1. 1Across multiple teams we’re standardizing on a worker‑first approach. How would you define a cross‑team strategy for handling persistent data, given workers cannot directly use localStorage, while ensuring consistency, security, and offline support?
  2. 2Consider a large SaaS product that must support both modern browsers and older ones without Web Worker support. How would you design an abstraction layer that gracefully falls back while keeping the same API for storage access?
  3. 3What are the long‑term maintenance risks of coupling business logic to the main thread solely for storage, and how would you refactor the architecture to decouple storage concerns from worker logic?

Follow-up Questions

  • What storage APIs are available to a worker without involving the main thread?
  • How does the same‑origin policy influence communication for persisting data?
  • Can delegating all storage to the main thread become a performance bottleneck?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.