with React and Web Workers
Mehdi Vasigh
twitter: @mehdi_vasigh
Engineer @ Mailchimp
♥️ creative coding
👩🍳 UI chef
I love meeting people!
Twitter: @mehdi_vasigh
We want to paint 60 frames each second
Browser takes ~4-6 ms per frame to do work
Our JS for each frame should take
~10-12 ms or less
JavaScript
Style
Layout
Paint
Composite
https://developers.google.com/web/fundamentals/performance/rendering
16 ms
JavaScript
Style
Layout
Paint
Composite
Browser can skip steps if nothing changes
16 ms
JavaScript
Style
Layout
Paint
Composite
16 ms
UI/Local Logic
JavaScript
Style
Layout
Paint
Composite
16 ms
CSS-in-JS Runtime
UI/Local Logic
JavaScript
Style
Layout
Paint
Composite
16 ms
CSS-in-JS Runtime
UI/Local Logic
Front-end Framework
JavaScript
Style
Layout
Paint
Composite
16 ms
CSS-in-JS Runtime
UI/Local Logic
Front-end Framework
State Management Logic
We've exceeded our frame budget and introduced jank
JavaScript
Style
Layout
Paint
Composite
16 ms
The more blocking JavaScript that we have, the more frames that we will drop
Bouncy Ball Fibonacci: web-worker-example.netlify.app
while (true) {
console.log('To infinity and beyond!');
}
// Will never run!
extremelyImportantFunction();
This is why fetch or setTimeout don't block the render
Jake Archibald - "In the Loop"
Web Workers allow us to do work in parallel using separate threads
When to use Web Workers:
Webpack
worker-loader: github.com/NativeScript/worker-loader
worker-plugin: github.com/GoogleChromeLabs/worker-plugin
Rollup
rollup-plugin-off-main-thread: github.com/surma/rollup-plugin-off-main-thread
Parcel & Vite
(Web Worker support is baked in! Hooray!)
comlink: github.com/NativeScript/worker-loader
Allows you to create "local instances" of objects living in a Web Worker
and use them as if they're any other object.
workerize: github.com/GoogleChromeLabs/worker-plugin
Allows you to move a module into a Web Worker and exposes exported functions as methods
useWorker: github.com/surma/rollup-plugin-off-main-thread
Similar to workerize, but as a React Hook
Depending on the nature of the code, you may not need to alter build system (i.e. using URL.createObjectURL and Blob)
Examples:
workerize: github.com/developit/workerize
comlink: github.com/GoogleChromeLabs/comlink
The Actor model: www.brianstorti.com/the-actor-model/
Treat different components of your UI system as "actors":
Example
Mehdi Vasigh - "Getting Started with JavaScript Web Workers and Off-Main-Thread Tasks"
dev.to/mvasigh/getting-started-with-javascript-web-workers-and-off-main-thread-tasks-4029
Surma - "Use web workers to run JavaScript off the browser's main thread"
Surma - "The main thread is overworked and underpaid"
www.youtube.com/watch?v=7Rrv9qFMWNM
Jason Teplitz - "Using Web Workers for more responsive apps"
Questions? Ask me here or on Twitter!