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.

    •  lemmyvore   ( @lemmyvore@feddit.nl ) 
      link
      fedilink
      English
      411 months ago

      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.

    • 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

  •  nous   ( @nous@programming.dev ) 
    link
    fedilink
    English
    34
    edit-2
    11 months ago

    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.

      •  nous   ( @nous@programming.dev ) 
        link
        fedilink
        English
        311 months ago

        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.

  •  Kissaki   ( @Kissaki@feddit.de ) 
    link
    fedilink
    English
    1211 months ago

    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.

    • 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.

  • 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.

    •  nous   ( @nous@programming.dev ) 
      link
      fedilink
      English
      311 months ago

      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.

        •  nous   ( @nous@programming.dev ) 
          link
          fedilink
          English
          211 months 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.

  •  tokyo   ( @tokyo@lemmy.ml ) 
    link
    fedilink
    English
    10
    edit-2
    11 months ago

    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.

    • If you use rust and structure your program correctly you can avoid debugging directly by building unit tests in language the verify behavior. Debugging tools are great and I look forward to better dx stories there (you can use chrome + DWARF to debug your native code) but strictly speaking it isn’t necessary most of the time.

  • 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.

  • 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.

    • 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.

    • 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.

  • 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.