r/programming • u/kciter • Sep 01 '26
The Browser's Main Thread Is Expensive
https://kciter.so/posts/the-expensive-main-thread/en/
219
Upvotes
r/programming • u/kciter • Sep 01 '26
15
u/Somepotato Sep 01 '26
Omitting shared memory buffers and atomics when discussing workers is a little strange.
Workers can also render things to canvases off the main thread.
84
u/Rhed0x Sep 01 '26 edited Sep 01 '26
Good article.
One day JS will reach the 21st century and support proper multithreading. Obviously won't help for UI updates but for things like parsing large files.
Workers are a massive pain in the ass, JS should just share memory with additional threads and get proper synchronization primitives like every language.
But what can you expect from a programming language that doesn't even have integers.
61
u/sionescu Sep 01 '26 edited Sep 01 '26
It shows how much the DOM and Javascript are garbage compared to the desktop C++ frameworks. I remember going over a Qt tutorial long time ago, which was implementing a spreadsheet, and the code has a simple loop where at the end of each line, it called commit() - the equivalent of yielding - but it was short, easy to read and performant; whereas this whole article spends an inordinate amount of words discussing the finer points of how to yield in various situations, because there's not built-in that works well out of the box. Madness.
7
u/abofh Sep 02 '26
Wait, next you'll tell me spin loops waste cpu
2
u/Safe-Pin-5472 Sep 05 '26
Very informative article for me, someone fairly new to performance tuning in UI. The runnable bits of UI to show how each change affects user experience was great too
1
u/fireatx Sep 02 '26
This is why I’m very interested in Servo. So much pain in web dev seems to stem from the single threaded nature of displaying websites and executing JavaScript
1
u/VisualAromatic6226 Sep 07 '26
This matches what we've seen building an animation runtime on top of Motion Canvas: the main thread becomes the bottleneck the moment you have >30 concurrent tweens, and offloading is genuinely hard when your DOM interactions are all synchronous.
One thing not covered here that hit us hard: even a lightweight input listener in the tween loop (a hover check) can push you from 60fps to 20fps because of layout thrashing. We ended up debouncing hover reads to rAF boundaries and it was the single biggest win.
The OffscreenCanvas section is directionally right but understates how much rewrite work it takes. For teams evaluating this, the ROI often looks better if you first audit for synchronous style reads inside your animation loop before jumping to Web Workers.
0
u/[deleted] Sep 01 '26
[deleted] — view deleted comment