DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Web Workers: True Multithreading in JavaScript
XLinkedInReddit
MediumFrontend Engineering

Web Workers: True Multithreading in JavaScript

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • Dedicated Workers
  • Shared Workers
  • Transferable Objects
  • Comlink: Simplified Worker API
  • What Workers CAN'T Do
  • Use Cases

JavaScript is single-threaded, but Web Workers let you run code in background threads. At Google, we offload heavy computation to workers.

Dedicated Workers

// main.js
const worker = new Worker("worker.js");
worker.postMessage({ type: "sort", data: hugeArray });
worker.onmessage = (e) => {
  console.log("Sorted:", e.data);
};

// worker.js
self.onmessage = (e) => {
  if (e.data.type === "sort") {
    const sorted = e.data.data.sort((a, b) => a - b);
    self.postMessage(sorted);
  }
};

Shared Workers

Shared across multiple tabs/windows of the same origin. Perfect for shared state like WebSocket connections.

const shared = new SharedWorker("shared.js");
shared.port.postMessage("hello");
shared.port.onmessage = (e) => console.log(e.data);

Transferable Objects

By default, postMessage copies data (structured clone). For large ArrayBuffers, use transfer to move ownership — zero-copy, instant.

const buffer = new ArrayBuffer(1024 * 1024 * 100); // 100MB
// Transfer (not copy) — instant, but buffer becomes unusable in main thread
worker.postMessage(buffer, [buffer]);

Comlink: Simplified Worker API

// worker.js
import * as Comlink from "comlink";

const api = {
  async processImage(imageData) {
    // Heavy computation
    return processedResult;
  }
};
Comlink.expose(api);

// main.js
import * as Comlink from "comlink";
const worker = new Worker("worker.js");
const api = Comlink.wrap(worker);
const result = await api.processImage(data); // Looks like a normal async call!

What Workers CAN'T Do

  • Access the DOM
  • Use window, document, or parent
  • Access synchronous XHR (don't anyway)

Use Cases

  • Image/video processing
  • Large dataset sorting/filtering
  • Encryption/hashing
  • Syntax highlighting (Monaco editor uses workers)
  • WebAssembly computation

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Dedicated Workers
  • Shared Workers
  • Transferable Objects
  • Comlink: Simplified Worker API
  • What Workers CAN'T Do
  • Use Cases

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.