0Pricing
JavaScript Academy · Lesson

Use Cases and Limitations

Know when workers help and what they cannot do.

Use Cases and Limitations is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Workers Are Good For

Web Workers shine when you have CPU-heavy work that would otherwise freeze the UI:

  • Image and video processing
  • Parsing large files (CSV, JSON)
  • Cryptography and hashing
  • Physics, data analysis, and simulations

No DOM Access

The biggest limitation: workers cannot touch the DOM. There is no document or window. They compute data and send it back; the main thread updates the UI.

// Inside a worker:
// document.querySelector(...) -> ReferenceError
// Workers have no DOM at all.

What IS Available

Workers still get many useful APIs: fetch, setTimeout, WebSocket, IndexedDB, crypto, and typed arrays. So data fetching and storage work fine off-thread.

// Inside a worker this is valid:
// const res = await fetch('/data.json');
// const json = await res.json();
// self.postMessage(json);

Structured Clone Limits

Recall that messages are copied via structured clone. You cannot send functions, DOM nodes, or class instances with methods, only their serializable data.

// Cannot be sent:
// worker.postMessage(() => {});      // function - fails
// worker.postMessage(document.body); // DOM node - fails

Startup and Messaging Cost

Creating a worker and serializing messages has overhead. For tiny tasks the messaging cost can outweigh the benefit. Workers pay off for substantial, sustained work.

Keeping Workers Alive

Spinning up a new worker per task is wasteful. Reuse a long-lived worker and send it many messages, or maintain a small pool of workers for parallelism.

// Reuse one worker for many tasks:
const worker = new Worker('worker.js');
function run(task) {
  worker.postMessage(task);
}
// Avoid: new Worker(...) on every single call.

Worker Pools

To use multiple CPU cores, create several workers and distribute tasks among them. Track which are idle and assign incoming work to free ones.

const pool = [];
for (let i = 0; i < 4; i++) {
  pool.push(new Worker('worker.js'));
}
// Round-robin or queue tasks across the pool.

Sharing Memory

For advanced cases, SharedArrayBuffer lets threads share memory directly (with Atomics for safe access). It requires special cross-origin isolation headers and is easy to misuse.

// Requires COOP/COEP headers to be enabled.
// const shared = new SharedArrayBuffer(1024);
// Atomics.add(view, 0, 1); // safe concurrent update

Error Isolation

An uncaught error in a worker does not crash the page. It surfaces as an error event on the worker, which you should handle to log or restart the worker.

worker.onerror = (e) => {
  console.log('worker failed:', e.message);
  // optionally recreate the worker here
};

When NOT to Use Workers

Skip workers when the task is trivial, requires constant DOM updates, or is mostly waiting on the network (async fetch already does not block). Use them for genuine CPU-bound work.

Putting It Together

Web Workers trade direct DOM access and easy data sharing for true parallelism. Use them for heavy computation, communicate via messages, reuse workers, and transfer large buffers to keep your app fast and smooth.

Quick Check

Test your understanding of worker use cases and limits.

Recap

You learned worker use cases and limits:

  • Great for CPU-heavy work; no DOM access.
  • Many APIs (fetch, IndexedDB, crypto) are available.
  • Messages are structured-cloned, so no functions or DOM nodes.
  • Reuse workers or pools to amortize startup cost.
  • Errors stay isolated on the worker.

You finished Web Workers. Next, IndexedDB.

Frequently asked questions

Is the “Use Cases and Limitations” lesson free?

Yes — the full text of “Use Cases and Limitations” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Use Cases and Limitations”?

Know when workers help and what they cannot do. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Use Cases and Limitations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a Web Worker
  2. Messaging with postMessage
  3. Transferable Objects
  4. Use Cases and Limitations
← Back to JavaScript Academy