Port Authority

Port patterns / Streams, zero-copy data and RPC

Move 64 MB between origins without copying it

Post an ArrayBuffer and it gets structured-cloned. Put it in the transfer list and ownership changes hands: the sender's buffer drops to zero bytes, avoiding the JavaScript copy (moving bytes between two separate site processes still involves an IPC copy under the hood, whereas between a page and its own worker it's a true pointer move). Each button sends 64 MB to another origin and back and times the round trip.

When you'd use this

A browser photo editor runs a 48 MB pixel buffer (4000×3000 RGBA) through a worker or sandboxed filter plugin. Transferring the ArrayBuffer hands ownership across without doubling peak RAM; once the filter finishes, it transfers the same buffer back so the editor can paint it.

Code

port.postMessage(buffer);            // structured clone: a copy each way
port.postMessage(buffer, [buffer]);  // transfer: buffer.byteLength is now 0

In practice

// photo editor transferring raw RGBA pixel data to a filter plugin
const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
const buffer = image.data.buffer;       // 48 MB for 4000×3000 RGBA
port.postMessage(buffer, [buffer]);
// buffer.byteLength is now 0 — no duplicate 48 MB copy in JS heap

// plugin mutates the bytes and transfers the exact same buffer back
port.onmessage = ({ data: buffer }) => {
  sepia(new Uint8ClampedArray(buffer));
  port.postMessage(buffer, [buffer]);
};

// host wraps the returned buffer in a fresh ImageData view
port.onmessage = ({ data: buffer }) =>
  ctx.putImageData(new ImageData(new Uint8ClampedArray(buffer), canvas.width), 0, 0);

How it works

Putting an ArrayBuffer in the transfer list hands ownership to the receiver instead of cloning it in JavaScript. Between a page and its own worker in the same process, it's a true zero-copy pointer move; between two separate site processes, the browser still crosses an IPC boundary, but avoids duplicating the buffer in JS. Either way, the sender's buffer immediately drops to byteLength === 0.

Without the transfer list, sending 64 MB copies every byte twice and doubles peak memory.

Because the sender can no longer touch the detached buffer, there's no shared mutable state and no locking required.

Step by step

What each press of Next does in the demo above.

  1. Open a channel. Transfers port2 to the frame on another origin.
  2. Transfer 64 MB. Moves 64 MB there and back without a JS copy. While it's in flight, this page holds 0 bytes.
  3. Copy 64 MB. Clones the same 64 MB instead of moving it so you can compare the round-trip time and memory held.

What you can build with it

Built and running below.

Reuse two buffers between a page and a worker without copying

Moving decoded images, audio buffers, or ML weights between workers. Ping-ponging a fixed pool of reusable buffers between two threads to avoid GC churn.

A worker needs somewhere to put each result, and making a fresh 8 MB buffer every time is wasteful. This page makes two, once. One goes to the worker, which fills it and sends it back, while the other is being read here. Then they swap. The counters show megabytes moved climbing and megabytes copied staying at zero.

Code

const pool = [new ArrayBuffer(SIZE), new ArrayBuffer(SIZE)];
worker.onmessage = ({ data }) => {
  use(data.buffer);                            // read the result
  worker.postMessage({ buffer: data.buffer }, [data.buffer]);   // and send the same memory back
};
for (const buffer of pool) worker.postMessage({ buffer }, [buffer]);

Step by step

What each press of Next does in this demo.

  1. Start the pool. Two buffers are made once, and then only moved.
  2. Stop. The pool stops. Two buffers were ever made.

A sandboxed calculation that hands back 16 MB without copying it

Returning large binary results from a sandboxed computation.

The frame is sandboxed and on another origin, which is where you want code you did not write. It marks every prime below 16.7 million in a 16 MB buffer. Posting that back the ordinary way makes a second 16 MB and takes time that grows with the size. Posting it with the buffer in the transfer list moves the memory itself. The frame measures how long each call took, and how much it has left afterwards.

Code

// in the sandbox
const sieve = findPrimes(16 * 1024 * 1024);                // a 16 MB Uint8Array
port.postMessage({ sieve }, [sieve.buffer]);               // moved
sieve.byteLength;                                          // 0: it is the page's now

Step by step

What each press of Next does in this demo.

  1. Sandbox: work it out and send a copy. The frame computes, then posts the result the ordinary way. It keeps its 16 MB, and the page gets another.
  2. Sandbox: work it out and move it. The same work, and this time the buffer is in the transfer list.

Things to try

  • Compare the round-trip time of Transfer 64 MB vs Copy 64 MB.
  • Notice the sender's byte count drops to 0 bytes during transfer vs 64 MB held during copy.

Gotchas

  • Forgetting the transfer list silently clones the buffer with zero warning.
  • Transferring view.buffer transfers the entire underlying ArrayBuffer, not just a typed-array sub-slice.
  • Typed arrays pointing at a transferred buffer immediately have length 0.
  • If both threads genuinely need simultaneous access to the same bytes, you want a SharedArrayBuffer on a cross-origin isolated page.

Measured

Chromium 157: 64 MB round-trip to a sandboxed frame in a few milliseconds by transfer. Other browsers are being filled in from the lab's results.

References

The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.

ArrayBuffer
A block of raw bytes. It can be moved to another thread without copying. MDNECMAScript
Transferable objects
Objects that can be moved to another page or worker instead of copied. After the move the sender can no longer use them. The transfer list is the argument to postMessage that names them. MDNHTML Standard
SharedArrayBuffer
A block of raw bytes that several threads can read and write at the same time. Posting it shares it instead of copying it. MDNECMAScript
Cross-origin isolation
A state a page enters by sending two headers. It keeps other origins out of the page's process, and the browser then allows shared memory. MDNHTML Standard

For more, see Further reading: articles, talks, papers, libraries and the specifications, collected in one place.