๐ท 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:
- StarsNPMTSMaintained
- 10k270k/w2 years
- 2k5M/w
- 3k92k/w3 years
- N/A120k/w
- 2k15k/w
- 4k700/w5 years
- 6005k/w5 years
- 5k600/w5 years
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))