Web Workers, comlink, TypeScript and React | johnnyreilly
JavaScript is famously single threaded. However, if you're developing for the web, you may well know that this is not quite accurate. There are Web Workers: A worker is an object created using a constructor (e.g. Worker()) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window. If you're using Vite to build your React app, you may prefer to read this post. Given that there is a way to use other threads for background processing, why doesn't this happen all the time? Well there's a number of reasons; not the least of which is the ceremony involved in interacting with Web Workers. Consider the following example that illustrates moving a calculation into a worker: This is not simple. It's hard to understand what's happening. Also, this approach only supports a single method call. I'd much rather write something that looked more like this: There's a way to do this us
JavaScript is famously single threaded. However, if you're developing for the web, you may well know that this is not quite accurate. There are Web Workers : A worker is an object created using a constructor (e.g. Worker() ) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window. If you're using Vite to build your React app, you may prefer to read this post . Given that there is a way to use other threads for background processing, why doesn't this happen all the time? W
Explore this link on the map →saved by
related reading
- Web Workers API - Web APIs | MDNdeveloper.mozilla.org
- Grainradgrainrad.com
- Reading 18: Message-Passing and Networkingweb.mit.edu
- The Conductor Rewrite: What They Changed to Make It Fastperformance.dev
- You Might Not Need an Effect – Reactreact.dev
- Markov's Salonmarkovs-salon.vercel.app
- 500 Lines or LessWeb Spreadsheetaosabook.org
- Making Sense of React Server Components • Josh W. Comeaujoshwcomeau.com
- Tutorial: Tic-Tac-Toe – Reactreactjs.org
- Reading 15: Promisesweb.mit.edu
- Service Worker API - Web APIs | MDNdeveloper.mozilla.org
- TypeScript Structured Concurrencythecandidstartup.org