Rara runs the sack race one step per frame really painted: count the votes on the main thread or in a real Worker, and see what happens to the race; plus a 32…
Why it matters
A page’s JavaScript and its painting share one thread. One heavy loop freezes everything: animation stops, clicks go unanswered. A worker moves heavy work to another thread, but with no document, a cost per message, and data that is copied unless transferred.
The key idea
A worker frees the main thread, it does not speed the work up
Common mistakes
The worker crashes on document A worker has no DOM, no window and no document. Count in the worker, send the result with postMessage, and let the main thread update the page.
Sending big data to a worker makes it slowerpostMessage copies the data (a structured clone) on the main thread, and that is expensive for big data. Move an ArrayBuffer with the transfer list: postMessage(buf, [buf]), with no copy.
A worker for tiny work is slower Making a worker and sending messages has a cost. Work under a few milliseconds is quicker on the main thread, or split up with requestIdleCallback. If you still use a worker, make it once and reuse it.
Try it in this playground
Start the race with Arka counting Rara stops; almost no frames while counting.
Move the count to the tent The same work, but frames keep coming and Rara keeps hopping.
Send the 32 MB box: copy it, then transfer it A copy holds the main thread a few ms; a transfer takes almost 0 and leaves the box at 0 bytes.
Have the worker write the board with document An error from the worker itself: there is no document there.