explaingit

googlechromelabs/comlink

12,655TypeScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

Tiny JavaScript library from Google Chrome Labs that makes web worker background threads as easy as calling regular functions, hiding complex message-passing behind simple async/await syntax.

Mindmap

mindmap
  root((comlink))
    Problem Solved
      Single main thread
      UI freezes
    How it works
      Hides postMessage
      Async await calls
      Promises returned
    Features
      Callback support
      Binary transfer
      Shared workers
    Tech Stack
      TypeScript
      Web Workers
      Browser native
    Audience
      Frontend developers
      Performance engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Move expensive calculations off the browser's main thread to keep the user interface responsive during heavy processing.

USE CASE 2

Run file parsing or data transformation in a background worker without writing complex postMessage boilerplate.

USE CASE 3

Share a single background worker thread across multiple open browser tabs using a shared worker.

USE CASE 4

Transfer large binary data such as images or audio buffers between threads without copying them, using the browser's transfer mechanism.

Tech stack

TypeScriptJavaScriptWeb Workers

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

Web browsers run JavaScript on a single thread by default, which means every calculation, animation, and user interaction competes for the same execution slot. When a page does something computationally heavy, like processing a large file or running complex calculations, the interface can freeze or become unresponsive because that single thread is busy. Web Workers are a browser feature that lets you run JavaScript code in a separate background thread, keeping the main thread free for user interactions. The problem is that communicating between threads requires a message-passing API called postMessage, which is low-level and awkward to work with. Comlink is a small library from Google Chrome Labs that hides that complexity. Instead of manually sending and receiving messages, you write code that calls functions on a worker almost as if they were regular local functions. Comlink translates those calls into the underlying message-passing automatically. Because the communication is still asynchronous (one thread cannot directly read the other's memory), your function calls return promises, which you wait for using the standard async/await syntax in JavaScript. The practical result is that moving expensive work off the main thread becomes much less code and much less mental overhead. The library is tiny at about 1.1 kilobytes compressed, so adding it to a web page has virtually no size impact. Comlink also handles passing callbacks between threads, transferring large binary data efficiently without copying it (using the browser's built-in transfer mechanism), and working with shared workers (a variant of web workers that multiple browser tabs can connect to simultaneously). The library is maintained under the GoogleChromeLabs organization on GitHub, which publishes tools and experiments from the Chrome team. It works in all modern browsers including Chrome, Firefox, Safari, and Edge. The README includes working code examples and documents the full API. No server-side component is needed, this is entirely a browser-side tool for improving the performance and responsiveness of client-facing web applications.

Copy-paste prompts

Prompt 1
Using Comlink, move a function that parses a 50,000-row CSV string into a web worker so the browser UI stays responsive. Show the worker file and the main thread call using async/await.
Prompt 2
I have a React component that runs a slow image-processing function on the client. Rewrite it to use Comlink and a web worker so the main thread is never blocked during processing.
Prompt 3
Show me how to use Comlink to expose a class from a shared worker so that two different browser tabs can call its methods and share the same state.
Prompt 4
Using Comlink, pass a callback function from the main thread into a web worker so the worker can report progress back while processing a large dataset.
Prompt 5
How do I use Comlink with a bundler like Vite to set up a typed web worker that exports a strongly-typed API callable from the main thread?
Open on GitHub → Explain another repo

← googlechromelabs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.