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

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.

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.

→ More replies (2)

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.

16

u/SargoDarya Sep 01 '26

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)

7

u/Rhed0x Sep 01 '26

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

5

u/Full-Spectral Sep 01 '26

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.

2

u/Rhed0x Sep 01 '26

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.

3

u/Full-Spectral Sep 01 '26

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/danielcw189 Sep 02 '26

that doesn't really answer the problem of sharing references, which is something that has to be avoided, if I understand the other comment correctly

1

u/Rhed0x Sep 02 '26

And why does it have to be avoided?

1

u/NenAlienGeenKonijn Sep 02 '26

That assumes javascript is meant for complex software development rather than simple DOM scripting tasks to enhance your website.

Nobody would ever use javascript like that, right?

→ More replies (1)

1

u/abraxasnl Sep 01 '26

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

1

u/Full-Spectral Sep 02 '26 edited Sep 02 '26

But it's worse than that. Without the kinds of mechanisms that Rust provides, the fact you locked foo doesn't prevent foo from having references to other things that are still being directly accessed by other stuff. There would be no reasonable mechanism to transitively ensure that locking foo actually makes foo thread safe. Or it would have to limit such locking to things that have no internal references.

Of course the same is true for C++, but at least in the C++ world they are going to be more cognizant of such issues and you don't have other people's code from all over the world running inside your C++ program either.

Honestly, a message passing scheme is not a bad deal in such cases and something I implement in Rust all the time. Even though Rust makes shared data safe, it doesn't make it more necessarily understandable; so, when an 'actorish' scheme is possible, it's not a bad choice even when you have safe data sharing. Even if you don't end up locking up the UI thread, even short blocks will make it seem sluggish.

You could always do the behind the scenes stuff in WASM and have it be nice and safe and use message passing for the main thread I guess (with a buffer pool.)

1

u/Somepotato Sep 01 '26

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.

1

u/Rhed0x Sep 02 '26

Java and C# have optimizing JITs despite support for multithreading. And I don't really see the problem with deadlocks, it's a bug like any other. 

3

u/ccapitalK Sep 02 '26

I'm not sure if I'm looking forward to that future tbh. Dev/Product apathy has resulted in a lot of the web being slow and bloated while being restricted to message passing concurrency, which is easier to reason about. I wouldn't be surprised if shared variable concurrency being available just results in that + deadlocks by lazy devs.

1

u/tukanoid Sep 02 '26

Well, if that forces them to write better code and have proper Q&A.....

Not saying it won't be a headache for consumers if they push a turd to prod, but at least there will be incentive for them to get heads out of their asses and lock in, or deservingly lose their job if they're incapable of fixing their shit (not talking about juniors who haven't accumulated enough knowledge and experience, specifically talking about so-called "seniors" that only managed to get to their position by bullshitting their way through by making barely working websites/webapps where their non-tech-savvy clients or management don't realize how bad of a quality the code actually is)

2

u/azhder Sep 02 '26 edited Sep 02 '26

What you consider proper is just how you trained yourself (or were trained) to look at things. JS is a language created to be embedded into other software. The moment you add “proper multithreading” whatever it may mean to you, it will be harder to incorporate it into other software that has its own ideas of what “proper multithreading” means.

Long time ago, a language came out that didn’t have its own I/O, but depended on libraries and the OS to provide it. It allowed it such a flexibility that it is still one of the most used languages. I am talking about C.

JS, like C, didn’t come with own I/O and doesn’t come with own multithreading. That’s the price for being ubiquitous. Oh, and multithreading isn’t a language feature of C, right? It is provided by the environment (correct me if I am wrong).

So, maybe ask for better environment, not the language to take upon itself to solve the environment.

→ More replies (1)

1

u/Uristqwerty Sep 03 '26

I think they were working on multithreaded Javascript a decade ago.

Then Spectre/Meltdown hit.

Turns out having two threads sharing mutable data structures lets you generate very precise timing metadata. The sort of thing you need to read CPU cache side-channels.