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
saved by
related reading
- Build your own Reactpomb.us
- Web Workers API - Web APIs | MDNdeveloper.mozilla.org
- Why React Re-Renders • Josh W. Comeaujoshwcomeau.com
- You Might Not Need an Effect – Reactreact.dev
- Making Sense of React Server Components • Josh W. Comeaujoshwcomeau.com
- Service worker overview | Workbox | Chrome for Developersdeveloper.chrome.com
- RSC From Scratch. Part 1: Server Components · reactwg server-components · Discussion #5github.com
- Understanding useMemo and useCallback • Josh W. Comeaujoshwcomeau.com
- What the heck is the event loop anyway? | Philip Roberts | JSConf EUyoutube.com
- The service worker lifecycle | Articles | web.devweb.dev
- TypeScript Structured Concurrencythecandidstartup.org
- The Event Loop and Callback Hell - Wes Boswesbos.com