Hacker News new | ask | show | jobs
by apatheticonion 710 days ago
It's crazy that people say that WASM won't replace JavaScript. This demonstrates a use case better suited for languages with better threading models than JavaScript.

Feels like the web world is desperately in need of competent threading capabilities. Can I (practically) use Rust or Go in the browser via wasm already!?

5 comments

The language has nothing to do with it.

You can use “WASM threads” from Rust or Go. Or JavaScript for that matter. Under the hood it’s all workers and shared array buffer, nothing to do with the language. Last time I checked you can’t even use std::thread for instance in Rust, since the compiler doesn’t understand workers (because workers aren’t actually part of WASM).

None of which will help you when updating the DOM anyway, since that happens in its own thread. It’s up to the browser how they run their rendering engine and I doubt they’re ever likely to allow direct control over that.

> Can I (practically) use Rust or Go in the browser via wasm already!?

You can (built into rust and tinygo for go.)! And wasm-threading has been around for a while! https://webassembly.org/features/ But I don't think these wasm-threading capabilities are well harnesses by any languages yet.

There been a decade of webworkers being super possible. That does get multi-threading! With transferable memory & everything. So, this isn't like some new super non-js thing. Some folks do use workers, but alas not popular enough, not something React or Angular helps steer folks to.

Whats super neat and tricky here is that the iframe in separate origin-agent-cluster has a full DOM implementation that's there & raring to go. We already could have tried using jsdom or happy-dom or a wasm-powered alternative in a worker to do something like this, already. This is super cool though because it's much lower code! It uses a dom runtime that's already loaded & available (the browser's) rather than having to ship & load a separate DOM runtime.

This use case is purposefully hamstringed for demo purposes. Rust or Go won’t change anything here, you still need to pass messages to/from the worker. Same exact mechanism as if you used a webworker, which are already widely available.

But to answer your question, yes you can use Rust and Go practically in the browser. It just isn’t all that helpful except in narrow circumstances.

Fair, you're right that typically you'd have a dedicated UI thread with workers handling business logic.

The difference with Rust and Go is they have better synchronization capabilities - where JavaScript largely forces you to clone data between threads.

So while you may still be messaging worker threads in Rust/Go - threads share memory which is fast and you have access to things like atomics and mutexes.

We can use Rust in the browser today with WASM and it's super cool - but without something like WASI, extensive thunking through JavaScript is required, plus there are issues with threading.

I hope that one day we can initialize a wasm module via a script tag with the browser offering a wasi interface

`<script src="module.wasm" type="application/wasi">`

And that browsers allow for threading without the restrictive security headers we have today

There is a proposal to add threading to WASM itself

https://github.com/WebAssembly/threads/blob/main/proposals/t...

But that’s not going to help when accessing the DOM.

Oh nice! That will be helpful. I guess WASI doesn't help with DOM access either - that's more about file access and such?

Is there a proposal(s) covering DOM access?

I remember interface types were a thing a few years back. If you have access to a dom object (I assume by pointer?) and it is removed/GC'd - would the wasm module have a null pointer?

> JavaScript largely forces you to clone data between threads.

Isn't that what SharedArrayBuffer is for (avoiding)?

Indeed. And there’s a whole Atomics namespace in the standard library for thread safety while using it.

Again, there’s very few actual uses for wasm. The main one is when you have a preexisting library in a different language, other than that you don’t gain any real functionality from using it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

SharedArrayBuffers are helpful for simple data types but because they are a simple fixed size array of bytes, you cannot share things like objects, class instances, arrays, etc and you need to manually extend the SAB by creating multiple SAB "pages" and manually glue them together. Ultimately it's not useful for most practical use-cases.

In the past I have used a SharedArrayBuffer to store a custom Graph implementation sent between threads. I stored the adjacency list in the SAB with the rest of the data being stored as a plain object and cloned/sent via postMessage between threads.

This worked but is extremely janky, required an unsafe custom data structure, manual/unsafe memory page management, was difficult to read/review and the performance was lacklustre because half of the data structure was plain JS objects that needed to be serialized and cloned anyway.

Simply, JavaScript sucks for multithreading and it would be great to have the ability to write high performance, multithreaded, complex web applications in a language designed with parallelism in mind - like Rust or Go.

While wasm _today_ cannot offer that (at least not practically) - there is certainly a use case for replacing JavaScript in that context and I hope that it does.

JS will still be useful for basic applications that just need a little scripting

SAB solves the problem of having a small number of dedicated workers that efficiently handle specific well constrained data processing tasks.

I must question whether the problem of random sites wanting to spin up a whole ton of cores that consume arbitrary amounts of compute and memory really needs to be solved.

Even with multithreaded WASM, the DOM will still only be usable from the page's (or iframe's) main thread, so if you want to do DOM operations not on the page's main thread you'll still need to do the same steps OP's project does (create an iframe and do stuff there).
Call me crazy but, WASM won't replace JavaScript. There are so many cases where a simple script will do. Firing up a terminal, writing in another language, compiling and going through the trouble to turn something simple into WASM is actually the crazy thing to do. Also, I am in no way approving of the techniques described in the article.
Yeah but most big web apps aren’t simple. If wasm ever replaces JS in a significant way, it won’t be replacing simple scripts. It’ll be replacing webpack lol
>It's crazy that people say that WASM won't replace JavaScript.

>big web apps

So you've defined the goalpost as "big web apps". There are billions more use cases for Javascript than however many "big web apps" might exist. So your phrasing is too broad, maybe consider:

>>It's crazy that people say that WASM won't replace JavaScript in big web apps.

Nobody's saying that. You are free to use WASM for your "big web app". Nobody and nothing is stopping you.

WASM certainly has its place, but it also certainly won't replace JavaScript in most of the places JavaScript is used every day. "Big web apps" are (my guess) maybe 1,000,000 of the 49,501,698 websites that use JavaScript, but it would depend on how you define "big web app" (and I really don't want go down that goal-posting rabbit hole). There are 13.8 million people writing javascript every day, how many of them do you think are working on "big web apps" that actually need WASM? How many of them do you think want to switch to Rust from Javascript? "developers" always seem to think everyone else should be some rockstar 10x programmer, but most use cases for javascript are pretty simple, and yet totally effective for what the requirements typically are.

Damn I’m just playing devils advocate for your point of “WASM won’t replace JS because there is a build process”.

I don’t think everybody needs to be building big web apps, I just think that most full time developers writing JS (or TS) are probably on a team working on a web app, so the introduction of a build process isn’t really an issue.

Of course it’s valid to write good ol’ javascript. I do it all the time. I’m always happy to avoid dealing with webpack

I do love the idea of a dedicated compiler handling my builds. Even with the most modern build tools today, large web applications can take minutes to build which doesn't include type checking, linting and testing. If you're not prioritizing build times as your app grows, this can quickly balloon.
Why would wasm be needed for build time tools? We already use fast compiled languages for modern webpack replacements (esbuild, swc, Bun :: Go, Rust, Zig), and they don’t use wasm.