Menu

The Return Trip: What Sending a Big Result Back From a Web Worker Costs the Main Thread
📰

The Return Trip: What Sending a Big Result Back From a Web Worker Costs the Main Thread

DEV Community: frontend·FindUtils·about 8 hours ago
#tkMdY3Pi
#devtofrontend#devto#dev#result#worker
Long article (2102 words) — no AI summary yet.
Reading 0:00
15s threshold

The usual advice for a page that freezes during heavy work is to move the work into a Web Worker. It's good advice. In an earlier test, heavy jobs on the main thread (a 39 MB CSV, a 24-megapixel photo, a 400-page PDF) froze the page for 0.2 to 0.6 seconds, while the same jobs in a worker kept every frame on time. I also measured the trip into the worker: transferring a 256 MB buffer took 0 to 10 ms, copying it 179 to 248 ms.

After that piece, a reader pointed out the half I hadn't measured: the result has to come back. The worker's whole point is a result the page can show, and that result travels to the main thread through postMessage too. If it gets copied, the copy is unpacked on the main thread , which is exactly the thread you were trying to protect.

So I measured the return trip.

The short version:

A copied buffer froze the page for up to 317 ms at 256 MB in Chrome (up to 200 ms in Firefox).…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.