Browser troubleshooting

Use a worker to preserve interface responsiveness

A Web Worker can run CPU-heavy processing away from the page’s main thread. It can make the interface more responsive, but does not automatically reduce total memory or make every task faster.

Worked example

Browser console example · use a disposable page on your own site

const source = "onmessage = event => postMessage(event.data.reduce((a,b) => a+b, 0));";
const url = URL.createObjectURL(new Blob([source], {type: "text/javascript"}));
const worker = new Worker(url);
try {
  const result = await new Promise((resolve, reject) => {
    worker.onmessage = event => resolve(event.data);
    worker.onerror = reject;
    worker.postMessage([1, 2, 3]);
  });
  console.log(result);
} finally { worker.terminate(); URL.revokeObjectURL(url); }

How it works

The worker receives a cloned message and sends a result back. The example uses a small fixed Blob source so it is self-contained; deployed applications often use a dedicated worker file. Cleanup terminates this one-shot worker.

Limits and mistakes

Content Security Policy may restrict Blob workers. Message cloning can add memory and transfer overhead, and workers cannot manipulate the DOM directly. Use transferable buffers where appropriate and validate messages at the boundary.

Check the result

Expect 6 in a browser that permits this worker. Try the real workload while interacting with the interface and compare responsiveness rather than only total elapsed time.

Sources

Reference documentation for the behavior used in this guide: