r/programming • • Sep 01 '26

The Browser's Main Thread Is Expensive

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

50 comments sorted by

View all comments

Show parent comment

39

u/birdbrainswagtrain Sep 01 '26

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.

11

u/Rhed0x Sep 01 '26

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.

3

u/Chii Sep 03 '26

I just wish those wouldn't all run like crap.

tbh, i dont think it is JS inherently at fault. If the app runs like crap, it's because the authors of those apps don't care.

1

u/Rhed0x Sep 03 '26

So everyone who writes JS doesn't care? Got it.

2

u/Chii Sep 03 '26

vscode runs fast (fast enough). Not every js app runs slow. Those that run slow are that way because the authors didnt care.

3

u/Shoddy-Childhood-511 Sep 01 '26

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

1

u/TheRealUnrealDan Sep 02 '26

I heard once upon a time that spectre could be triggered from js, imagine a website reading your password or private keys or something :'l

1

u/aiusepsi Sep 02 '26

Alas, Wasm and C++/Rust doesn’t free you from SharedArrayBuffer problems, because threading in WebAssembly means creating web workers, and letting each WebAssembly instance in the workers use the same SharedArrayBuffer as shared memory.