this post was submitted on 07 Aug 2023
85 points (98.9% liked)

Programming

17373 readers
292 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
 

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?

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 56 points 1 year ago (7 children)

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.

[–] [email protected] 5 points 1 year ago

But other than that, Mrs. Lincoln, how was the play?

I'm amazed at how much you described in a single sentence there

[–] [email protected] 5 points 1 year ago

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

load more comments (5 replies)
[–] [email protected] 42 points 1 year ago* (last edited 1 year ago) (6 children)

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.

[–] [email protected] 3 points 1 year ago (1 children)

What about maintainability of large code bases? JS even with TS tacked isn't so great or at least not as good as Rust.

[–] [email protected] 3 points 1 year ago (1 children)

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.

[–] [email protected] 1 points 1 year ago

Interesting take!

load more comments (5 replies)
[–] [email protected] 23 points 1 year ago

I've ported games to web using WASM. You still need to interact with HTML DOM using JS, no way around it.

You use WASM when you either need raw CPU speed, or you have some C++ code that you don't want to rewrite in JS.

If you just want to make a website, pure JS is better, unless you're that kind of dev who prefers to render their own text strings pixel by pixel.

[–] [email protected] 16 points 1 year ago* (last edited 1 year ago) (1 children)

The truth is that JS is currently "good enough" and all the best (adopted) web frameworks are either server or JS based.

I believe the chunking of script files is currently a bit more natural as well.

WebAssembly is the best choice for certain kinds of apps but most web apps are good enough with JS. If communities pour a lot of polish into WASM frameworks you may start to see wider adoption. Diversity is good, but it does need to be asked why WASM + DOM is objectively better than JS + DOM. It complicates the ecosystem a bit because you might fracture it for no good reason. Should there be Rust, Python, and JS DOM rendering frameworks? Is there a benefit?

If you have a more traditionally native app you want to port, that's different. That's a great fit for WASM. Personally I see it becoming more popular when it's a good replacement for desktop technology and the DOM isn't used at all (go straight to GPU). I'm a huge fan of WASM, but I also write a lot of web apps and don't see a super convincing reason to adopt WASM to effectively make the exact same thing. As-is, it's great for augmenting an app though.

Wait for garbage collection and sockets and you might see the paradigm start to shift.

[–] [email protected] 3 points 1 year ago (1 children)

This is it.

WebAssembly AFAICT is all about making existing code runnable in a JavaScript environment. JavaScript isn't great itself, but Chrome provides pretty amazing tooling, so it's good enough.

Add to that: if you want to write WASM in a strongly typed language, you need support libraries that define all of the browser primitives. If you're an accomplished web developer, it's more effective to stay in js.

[–] [email protected] 2 points 1 year ago

WebAssembly AFAICT is all about making existing code runnable in a JavaScript environment.

I would not say that. It is one aspect of web asm, but not the only or even main reason. There are far more frameworks and tooling out there designed for writing new stuff in it rather than getting existing stuff to run.

if you want to write WASM in a strongly typed language, you need support libraries that define all of the browser primitives.

These already exist for some languages - notably rust (but not exclusively). You can now write a web app fully in rust and have it preform on par with popular JS frameworks. There is JS involved in this - but it can all be generated so developers never need to actually touch it.

If you’re an accomplished web developer, it’s more effective to stay in js.

This is true, and the bigger reason why wasm is not as popular. If you already know a JS web framework there is little point in learning a whole new language as JS is good enough for most applications. And a whole new language and tooling is a large investment. But it does mean that people in other eco systems or that don't really like JS are now able to write webapps in other languages (which by the very nature are going to be a fraction of web devs as these people will likely have avoided it where they can). Other eco systems are also a lot less mature than JSs eco system for web development.

[–] [email protected] 16 points 1 year ago (1 children)

Replacing costs expertise, time, and money. Nobody wants to invest that for (to them) insignificant or even pointless reasons.

If you're using tooling, a framework, a library - each of those makes a switch more risky, costly, or impossible.

JavaScript works with the DOM. Why would you want to implement a separate WASM component that you have to interface with? JavaScript is good enough. Interfacing only brings problems with it.

When you use JS you are doing it right - because there is no other way to interface with the DOM. Anything else is built on top. How would you interface with WASM? Manually? Library? Framework? What programming do you use to compile to WASM with? How do you analyze and debug WASM when it executes in a compiled WASM-binary format?

The use case for WASM is performance, efficiency on CPU-bound operations. If CPU-bound performance is not a concern for you, or JS is good enough, there's little reason to use WASM. Other reasons are even more niche.

[–] [email protected] 3 points 1 year ago

The language best suited for wasm is easily rust. And you can still interface with the Dom using frameworks like yew sycamore or leptos.

Debugging is still a little tricky but you can debug wasm in chrome and DWARF allows you to have source maps that map to your rust code. This is s problem the community is working to improve. Until then you have the full power of console log which is how a large portion of developers already debug their applications.

[–] [email protected] 12 points 1 year ago (1 children)

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.

[–] [email protected] 3 points 1 year ago (1 children)

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.

[–] [email protected] 3 points 1 year ago (1 children)

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.

[–] [email protected] 2 points 1 year ago

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.

[–] [email protected] 11 points 1 year ago* (last edited 1 year ago) (4 children)

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.

[–] [email protected] 6 points 1 year ago* (last edited 1 year ago)

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.

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.

[–] [email protected] 3 points 1 year ago

There's Typescript, it's as close as you can get.

[–] [email protected] 3 points 1 year ago

You can absolutely modify the dom, you just need to go through a thin js glue layer between the dom and wasm

[–] [email protected] 2 points 1 year ago

For transpiled-to-JS languages: Elm, Purescript, Typescript

I personally use Elm. Really helps remove all the JS funny business, without really having to type it (mostly)

[–] [email protected] 9 points 1 year ago (2 children)

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.

load more comments (2 replies)
[–] [email protected] 7 points 1 year ago* (last edited 1 year ago) (1 children)

How will we be making WASM-based UI accessible for people using screen readers, screen zoom applications, text to speech and voice input users, etc.?

The Web is hostile enough to people with disabilities, despite its intent, and developers are already unfamiliar with how to make proper semantic and accessible websites which use JS. Throwing the baby out with the bathwater by replacing everything with WASM in its current form seems about as good an idea as Google's Web Environment Integrity proposal.

[–] [email protected] 7 points 1 year ago

JS has little to do with accessibility. Most web accessibility comes from the Dom and aria attributes as well as semantic tags. You can do all of that with wasm too.

Are you asking about how it will work with wgpu based applications? This will work the same as it does on desktop applications. The program calls out to libraries that support talking to screen readers. I know rust the language with the best support for and ecosystem around wasm libraries like this already exist and ui frameworks like egui already have some support built in.

[–] [email protected] 7 points 1 year ago

It's only smaller for large apps

[–] [email protected] 6 points 1 year ago

WebAssembly was never intended to replace JavaScript. It was intended to coexist with it. You still need something to initialize and load the binary.

[–] [email protected] 5 points 1 year ago (1 children)

So I gotta have to break out the xkcd comic again? Cause I will

[–] [email protected] 2 points 1 year ago

Do it do it do it! 🤣

[–] [email protected] 4 points 1 year ago* (last edited 1 year ago) (2 children)

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.

[–] [email protected] 5 points 1 year ago (1 children)

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.

[–] [email protected] 1 points 1 year ago

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.

[–] [email protected] 2 points 1 year ago

Here is an alternative Piped link(s): https://piped.video/watch?v=461735zrjzY

Piped is a privacy-respecting open-source alternative frontend to YouTube.

I'm open-source, check me out at GitHub.

[–] [email protected] 4 points 1 year ago

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.

[–] [email protected] 3 points 1 year ago

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.

[–] [email protected] 3 points 1 year ago

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.

[–] [email protected] 3 points 1 year ago

JavaScript is much more mature. It has a lot of established tools and frameworks aiding the developer. JavaScript frontend developers are also easier to recruit. You will just make everything harder for yourself by adopting WASM.

[–] [email protected] 3 points 1 year ago (1 children)

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.

[–] [email protected] 3 points 1 year ago

Here is an alternative Piped link(s): https://piped.video/4KtotxNAwME

Piped is a privacy-respecting open-source alternative frontend to YouTube.

I'm open-source, check me out at GitHub.

load more comments
view more: next ›