I've been wondering about this for a while and haven't really found a great answer for it. From what I understand, WASM is:
-
Faster than JavaScript
-
Has a smaller file size
-
Can be compiled to from pretty much any programming language
-
Can be used outside of the browser easier thanks to WASI
So why aren't most websites starting to try replacing (most) JS with WASM now that it's supported by every major browser? The most compelling argument I heard is that WASM can't manipulate the DOM and a lot of people don't want to deal with gluing JS code to it, but aside from that, is there something I'm missing?
The most compelling argument I heard is that WASM can’t manipulate the DOM and a lot of people don’t want to deal with gluing JS code to it, but aside from that
But other than that, Mrs. Lincoln, how was the play?
You've gotten several other answers that are true and correct - the pain of implementation at this point is greater than the pain points that WASM solves. But this is also a non trivial one - most of what Javascript should be doing on a webpage is DOM manipulation.
At some point, WASM will either come out with a killer feature/killer app/use case that Javascript (and all the libraries/frameworks out there) hasn't figured out how to handle, and it will establish a niche (besides "Javascript is sort of a dumb language let's get rid of it"), and depending on the use case, you might see some of the 17.4 million (estimated) Javascript developers chuck it for....what? Rust? Kotlin? C? C#? But the switching costs are non-trivial - and frankly, especially if you still have to write Javascript in order to manipulate the DOM....well, what are we solving for?
If you're writing a web app where one of the WASM languages gives you a real competitive advantage, I'd say that's your use case right there. But since most web applications are basically strings of api calls looped together to dump data from the backend into a browser, it's hard to picture wider adoption. I've been wrong before, though.
I'd imagine one of those killer features is using a language with a solid standard library. Npm dependencies are notoriously complex because js as a language is missing basic functionality that is standard in other languages. Just a few years ago the Internet broke because "pad left" was pulled by it's maintainer, that simply doesn't happen in other languages
From a maintenance perspective npm is a nightmare. From a security perspective it is worse. Being able to build your entire website using a language that eliminates most dependencies, and the ones you take on don't pull in a zillion dependencies either, is absolutely a killer feature
Of course that isn't the full story and using js still has it's advantages as people have already pointed out. If wasm closes the gap in those areas then it would absolutely be worth the switch
Even if WASM gets some amazing feature, like super-fast DOM manipulation, it would still be used via API from JavaScript. WASM is a subset of JavaScript that mainly consists of low level operations. It's not exactly nice for writing code. It's like assembler in this respect — very fast, very efficient, but we still tend to prefer a higher level language.
How is WebAssembly a subset of JavaScript? Aren't you confusing it with asm.js?
He meant it as "wasm will be invoked by javascript" and not the other way around.
Faster than JavaScript
For pure computation, using the right language it can be faster. For a general website that needs to manipulate the DOM the performance is about the same as what popular JS frameworks can do (and can be faster than popular ones like react). But there are faster JS frameworks that react already available and people are not flocking away from react to these other frameworks. So speed is not a big enough issue here for people to want to move to a new language with WASM.
Has a smaller file size
Not sure this is true. Maybe for a single function. But for a general application? I don't think so. WASM tends to be a bit larger than JS code I think as you often need to ship more code, where JS can rely more on things built into the browser. But we are at a point where this difference is not a huge concern any more either. So is not really a point for or against WASM here.
Can be compiled to from pretty much any programming language
This is a huge misleading point. Even if you could do it from any language not all languages have a ecosystem that is useable for it and a lot of languages require large runtimes that need to be shipped with the WASM bundle (making the points above far worst).
Can be used outside of the browser easier thanks to WASI
So can JS? And native code? So I don't really see what this statement is meant to be arguing for? It is irrelevant when talking about websites using WASM in the browser.
So why aren’t most websites starting to try replacing (most) JS with WASM now that it’s supported by every major browser?
Why should they start using it? They all have existing code, their devs already know JS. What major advantage would WASM give them over what they currently have? The points above I have already gone through and are not a big enough reason for this change outside of niche use cases. JS is good enough for most use cases and people that are already working in the web browser side of things already know it. There is little reason to make the switch to WASM as even in languages like rust, which likely has the most mature eco system, still has a vastly less mature eco system for web dev than JS.
There is no line that needs to be passed that will cause floods of people to start adopting it and start converting everything they maintain over towards it. If there is a good enough reason to adopt this technology then it will be done very gradually over many years if not decades. People wont suddenly throw out everything once some line is crossed without some extreme and unconditional benefit to doing so.
What about maintainability of large code bases? JS even with TS tacked isn't so great or at least not as good as Rust.
Yes, that is a good reason to use languages like rust overall. And one reason we are seeing quite a few web frontend frameworks being written in rust now. They are maturing quite fast and there is quite a bit of effort being put into them. But i believe most of this is coming from rust devs (that may have touched frontend tech before) wanting to use it in more places rather than JS devs wanting something more maintainable.
There is quite a lot you can do in languages like JS to help mitigate the maintainability issues as projects scale up in it. TS is one such thing that helps quite a lot and when employed well improves things quite a lot - enough of a difference that it makes the jump to a completely different language and tooling that moving to rust would involve become less attractive. There is still benefit of rust over TS, but also a much bigger cost than switching from JS to TS.
Some will see the switch as worth it, though more for newer/greenfield projects. Or those coming from backend already written in rust and thus already familiar with the language. Over time these types of projects and situations will grow - but that happens very slowly over time and not just because some line gets crossed by the technology. It also wont cause it to take over everything, but will just get a small market share of everything being written now.
This is also why we have so many different backend languages rather than one that everyone uses. If one language cannot take over that why would it work for the frontend? At best more websites will start suing other languages over time and slowly erode JSs market share in the web frontend space.
I’m genuinely confused by these responses. It’s as if most didn’t actually look into what WASM was besides a cursory glance and then answered right away.
First off WASM is (relatively) new. It’s at 1.0 which iirc is basically an MVP product. It will take years for all browsers to integrate it appropriately.
Why choose WASM over JS? You probably wouldn’t right now unless you wanted to help pioneer the technology. Again it’s fairly new and probably not expected to be used in professional environments yet.
As for the benefits, it’s mostly the speed of code execution. Yes JavaScript is fast and robust enough for current web apps. No it is nowhere near as fast as native code.
Think about PC games. When people need performance, JS is definitely not the first option or even one of them in most cases. You want a language closer to the metal which is why compiled languages like C++ are often used.
All that said, if it was in a mature phase and did run faster than JS, why would you care? Well with native compiled code, you could run some hefty programs from a browser with the speed of native code.
That potentially means running more intensive applications like games and photo editors completely on a website. You could bypass the need to download software. You would visit the website, the WASM code would be sent over and used in the browser to run the application.
You can also interact directly with JavaScript via a WASM and call WASM functions within JavaScript so it’s pretty connected.
Overall it’s a fairly new technology that when matured could mean a major change for how the web works. It will likely be a long time until we see it capable of being used professionally and even longer before we see widespread use.
I think the truth is that not only can't WASM manipulate the DOM, but javascript was build to manipulate the DOM and has been moulded around this purpose. Secondly, if you want to use WASM from another programming language, that is just another language you need to learn on top of javascript, because we are not at a stage where we can replace javascript (because of the DOM). Fo most it's more cost effective to just optimize their javascript code instead of adding another layer to the tech stack.
I think the truth is that not only can’t WASM manipulate the DOM
Not directly no. But it can via JS (without the developer needing to ever touch JS code themselves via auto generated bindings). And this is simply a limitation of the current WASM implementations/standards. I believe there is work on going to get WASM more direct access to the DOM. But really this will just be an efficiency improvement over what is currently possible (which is already possibly to be as fast as pure JS frameworks).
Secondly, if you want to use WASM from another programming language, that is just another language you need to learn on top of javascript, because we are not at a stage where we can replace javascript (because of the DOM).
This is not quite true. Yes we cannot replace JS currently. But you don't need to learn JS to develop using WASM - there is already enough tooling that can auto generate any JS code needed for you so developers never actually need to learn or touch JS if they don't want to.
But existing developers that already know JS don't have a huge reason to learn another language that only have a vastly less mature ecosystem. So the door is currently open for those that don't enjoy or want to touch JS, but those people will at this point in time be working on other things rather then web apps. So there is not going to be a huge push for moving to a different language with WASM support any time soon. Not to mention that there is only minor benefits to existing applications that make a full rewrite to another language not worth the effort. So people wont be flocking over toward it, but not for the reasons you mentioned.
Sure, you can autogenerated js bindings, but as soon as you need to start debugging or optimizing you need to understand the js that was generated for you.
I don't think so. Having played around with wasm-bindgen in rust as well as other libraries the build on top of it I have never once needed to look at or understand the generated JS code. When you need to debug or optimise things the first thing to look at is the rust code. So as it stands today I do not think you need to know JS to write code for a web frontend - at least in isolation of anything else (ie teams, existing products etc).
You will likely need to learn JS at some point when writing stuff for the web - as the JS ecosystem is so vast for web development when compared to anything else. But WASMs JS bindings are not the point you will need to know it for.
Unless it has improved since last time I used it then it's awful to debug in the browser. I hate JavaScript, but would probably still use it for a new web project now.
As a hobbyist web dev:
- I want to support niche browsers that might not support WASM such as Dillo or NetSurf.
- Most of the resources out there are Javascript-only.
- I don't even use scripting most of the time in order to be accessible to text-only, old and minimalist browsers.
I can also imagine that people who are more professional than me in their development require existing JS frameworks, libraries, tutorials and whatever, and were taught in Javascript; there is no reason for them to change. There is also probably no real money in developing WASM applications commercially.
From my experience JS is primarily used to manipulate the DOM. I haven't looked into it, but if you're correct that WASM cannot manipulate the DOM then your question, to me, is tantamount to "Why aren't people using forks to eat soup?".
I would love a staticly-typed, compiled language to come along and replace JS. If anyone is aware of how I can write Rust in place of JS, please let me know. For now, I suffer/enjoy JS.
There are quite a few web frontend frameworks for rust now that are reasonably mature. Though you might still find a few rough edges they are usable for projects now.
- https://www.leptos.dev/
- https://dioxuslabs.com/
- https://yew.rs/
All of these can work without you needing to write any JS code. Though there is JS glue code involved, it is generated and you don't need to worry about it.
But the JS eco system is still quite large and hard to completely avoid.
You can absolutely modify the dom, you just need to go through a thin js glue layer between the dom and wasm
IIRC a startup tried to exactly that... It's wasn't any faster and is actually harder to develop.
EDIT:
Sorry ignore these videos. I don't remember which YouTube video it is. But more importantly Rust + WASM got really better than JS these days.
-
https://www.youtube.com/watch?v=461735zrjzY
-
https://github.com/leptos-rs/leptos
The video is essentially saying the exact opposite of what you are saying. It's showing leptos to be much faster than react and I know primeagen doesn't think rust is harder develop.
My bad. I can't find the actually video but there exits a startup that shutdown because Rust/WASM performance wasn't any better on top of that it's was harder to develop with Rust. But as my edit to my previous comment shows things got better for Rust. It's no longer the case.
-
On the subject of dom manipulation from wasm I highly recommend this video https://youtu.be/4KtotxNAwME. It's from leptos author, one of the more popular wasm framework. The TLDR is that modifying the dom isn't the bottleneck for wasm.
Javascript is just really great. WASM is also great and really opens up some cool possibilities, but there is something inexplicably simple and approachable about Javascript. I think it will be very hard to topple it from its throne, even if WASM overcame it's DOM issues.
From a historical standpoint, there is also the bad blood of ActiveX, Flash, Silverlight and early Java applets that still leaves a bad taste in people's mouths. It has a slightly steeper uphill battle to fight.
Faster than JavaScript
JS is usually fast enough.
Has a smaller file size
It really depends. If you aren't careful, your Wasm blob can end up ballooning in size. If you start pulling in libraries and doing things like parsing JSON, your binary can get big, quick.
Wasm adds an extra layer of complexity that needs to be justified. In most cases, it's just not worth it.
Nothing is stopping them. Have you seen Leptos? https://github.com/leptos-rs/leptos