1. Web Storage API: localStorage vs sessionStorage
The HTML5 Web Storage API provides key-value pair storage directly within the user’s browser. It replaces legacy HTTP cookies for client-side data persistence by offering higher storage capacity (~5MB per origin) without transmitting data on every server request.
| Feature | localStorage |
sessionStorage |
|---|---|---|
| Lifespan | Persists indefinitely until explicitly deleted via code or cleared by user. | Expires automatically as soon as the tab or window session closes. |
| Scope | Shared across all windows/tabs with the same Origin (protocol://domain:port). |
Restricted strictly to the current browser tab and session. |
| Storage Capacity | ~5 MB per origin (varies by browser). | ~5 MB per tab session. |
Core Web Storage Methods
Test saving values to localStorage vs sessionStorage live:
2. Storage Scope & Cross-Tab window.onstorage Event
When localStorage is modified in one browser tab, other open tabs sharing the exact same origin receive a native storage event.
Web Storage Scope & Cross-Tab Synchronization Architecture
Listening for Cross-Tab Storage Changes
Click to simulate a storage update and watch the event listener inspect details:
3. Multithreading with Web Workers (new Worker())
JavaScript normally runs on a single main thread. Heavy computations (complex math, image processing, large datasets) can freeze the UI and create lag. Web Workers allow scripts to run in background threads without blocking the DOM thread.
DedicatedWorkerGlobalScope). They cannot access the DOM (no document, window, or parent access). However, they can use fetch, IndexedDB, WebSockets, and timers.
Instantiating a Web Worker
Click to spawn a live Web Worker thread and pass data across worker threads:
4. Worker Architecture & Message Passing
The Main Thread and Web Worker communicate asynchronously via postMessage() and the onmessage event handler.
Main Thread vs Web Worker Thread Architecture
Main Thread vs Worker Thread Communication Pattern
Test UI responsiveness while performing heavy loops on Main Thread vs Background Worker:
5. Interactive Project: Multithreaded Prime Calculator & Storage Cache
Build a high-performance Multithreaded Prime Number Calculator that offloads heavy mathematical computation to a Web Worker background thread and caches calculated results in localStorage.
Background Computation + Storage Caching Architecture
Compute prime numbers up to N in background thread with local cache:
Hands-On Challenge: Build an Offline Text Editor with Auto-Save Worker
Build an auto-saving note taking app that stores document drafts in localStorage every 3 seconds and offloads word count & character count statistics calculation to a Web Worker.
Chapter 18 Key Takeaways
- Use
localStoragefor persistent client storage; usesessionStoragefor single-session tab data. - Always stringify JavaScript objects with
JSON.stringify()before storing. - Listen for cross-tab changes using
window.addEventListener('storage', callback). - Web Workers allow running background threads (`new Worker()`) without blocking UI animation or DOM events.
- Web Workers cannot access DOM elements or global
windowcontext directly.