r/programming 8h ago

The Browser's Main Thread Is Expensive

https://kciter.so/posts/the-expensive-main-thread/en/
84 Upvotes

17 comments sorted by

39

u/Rhed0x 7h ago edited 7h ago

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.

11

u/birdbrainswagtrain 3h ago

It would be neat to see proper multi-threaded JS but I assume the amount of engineering and security considerations massively outweigh whatever benefits we'd get out of it. I'm still fighting obnoxious restrictions on SharedArrayBuffer.

If you're the rare person who actually needs to write a multi-threaded web application, and who needs shared state (message passing is a great approach for multi-threading which I suspect works for 90% of applications) I think you should probably just bite the WebAssembly bullet and use C++ or Rust.

4

u/Rhed0x 3h ago

I already avoid JS but unfortunately it's used for most desktop applications these days. I just wish those wouldn't all run like crap.

1

u/Shoddy-Childhood-511 1h ago

A priori, I'd think multi-threaded JS would enable stupidity that killed the CPU entirely.

Instead, we could throttle the shit out of JS maybe? Alternatively we could've certificate infrastructure for "We promise not to hog your CPU or else we'll loose this certificate forever" for sites that wanted a bit more CPU time, but that feels overly complex vs simply throttling all JS.

Anyways, multi-threaded JS or WASM could perhaps be exploited to hack your system, using techniques like rowhammer. Browsers should probably isolate multi-threaded WASM to one physical core, or roll back threads support entirely.

I should find the firefox version for Chrome's '--disable-webassembly-threads'. lol

12

u/SargoDarya 7h ago

You can already use a SharedArrayBuffer with postMessage though? Also it has BigInt (which isn’t int, I get that but you get the idea)

6

u/Rhed0x 7h ago

That requires manually converting stuff into PODO, why can't you simply pass JS references across to a worker.

3

u/Full-Spectral 6h ago

It is just all GC'd data and I'd think it would be easy enough. But, they'd have to ensure that nothing inside that reference, no matter how deep, was still visible to the sender. With the simplified scenarios they provide currently, that's doable. But unless they are going to create Rustscript, it would probably be hard to do with arbitrary objects.

1

u/Rhed0x 4h ago

they'd have to ensure that nothing inside that reference, no matter how deep, was still visible to the sender

Or they add Mutexes, Semaphores, etc and objects can just be shared across threads like in every other language.

1

u/Full-Spectral 4h ago

It would have to be done at an automagical level probably, given the nature of Javascript and the experience of a lot of developers of such. Otherwise, you'd have web sites seizing up right and left when they deadlocked themselves.

1

u/abraxasnl 18m ago

Because then you would need locking mechanisms, which are waaaay to easy to get wrong for users.

1

u/Somepotato 13m ago

Because that entails locking which can deadlock the main thread. Requiring you to be more explicit about it removes deadlocks as a risk. Plus the jit can make a number of optimizations when it knows someone rude can't step in and do a read unexpectedly.

4

u/sionescu 1h ago edited 50m ago

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.

1

u/Somepotato 13m ago

Omitting shared memory buffers and atomics when discussing workers is a little strange.

Workers can also render things to canvases off the main thread.

-10

u/[deleted] 6h ago

[deleted]

21

u/TankorSmash 6h ago

Did you get the AI to output it like this, or was it manually done?

6

u/Blashtik 4h ago

1 week old account. I doubt there's even a human in the loop.

16

u/occasionallyaccurate 6h ago

ai writing patterns detected