๐Ÿ‘ท Web Workers

If you have computation-heavy tasks in your app, you can offload them to a Web Worker. Workers are separate threads that runs in the background without blocking the UI, reducing jank in your canvas. You can for instance use Web Workers for pathfinding or physics.

Web Workers rely on the postMessage and onmessage API to communicate with the main thread, which means you cannot wait for a particular message to be received and write code in a convenient async/await fashion. Thatโ€™s why some libraries have been created to help with the process:

Compare on NPMTrends and Star History (requires a GH token)

About these numbers and colors

import.meta.url

Note that you can use import.meta.url to reference a Web Worker file from your filesystem:

const worker = new Worker(new URL('./worker.ts', import.meta.url))