Web Assembly (Wasm)

Why Web Assembly

Jem Young

Jem Young

Web Assembly (Wasm)

Check out a free preview of the full Web Assembly (Wasm) course

The "Why Web Assembly" Lesson is part of the full, Web Assembly (Wasm) course featured in this preview video. Here's what you'd learn in this lesson:

Jem explains that Web Assembly works in tandem with JavaScript because it allows languages like C and Rust to be compiled into a language the browser can understand. Questions about utilizing JavaScript APIs in Web Assembly and Web Assembly's similarities to other assembly languages are also covered in this segment.


Transcript from the "Why Web Assembly" Lesson

>> So this is the question I get asked a lot because people here aren't familiar with web assembly be like, cool cool cool. Web assembly, why do we need web assembly? Is it java script? And I am like well no, it's not JavaScript. Can I use on JavaScript?

Absolutely. You're supposed to, you have to. Is it going to replace JavaScript? No, it is not a replacement for JavaScript. Web assembly is designed to work in tandem with JavaScript. It's an entirely different language running an entirely different process in the browser. The main point of confusion that people get is should I use web assembly or a web worker?

And again, those are two different concepts. They're both different processes running in the browser. But web assembly is its own environment its sandbox is memory safe. And the main point of web assembly is that other engineers can write code that runs on browser. And this is the part that always gets me it's kind of a misnomer because people say we're suddenly like, yeah, it must be something in front engineers do.

And actually, from the people I've talked to you all the blog posts I've read the talks I've seen, most of the people writing web assembly are not front end engineers. They are people that write C. They're people to write rust or C++ or I don't know if C sharp compiles yet.

I'll look that up. But there are people that aren't necessarily enamored with the front end like like, we are in front of masters. There are people that they want to do video processing image processing games, because web assembly works with binary data. That is super low level ones and zeros and you're moving them around, which is why we're gonna spend quite a bit of time on the foundational work.

But really the advantages of web assembly are, it's fast. And I know you're thinking, is it faster than JavaScript? It depends. That's not really the use case and I'm sure lots of people on Twitter are gonna get inflames, and they're gonna get really angry about saying that. But it really depends on the use case on whether or not you should use web assembly or JavaScript for particular for solving a particular problem.

By the end of this course I hope and we will understand that the use case for web assembly versus JavaScript and when to actually take on the complexity of dealing with web assembly. Well, actually, I think I can do this in JavaScript and a web worker. More advantages of web assembly in general, I've already said it, you can take a high level language like rust or C, it compiles down to web assembly.

It runs on more than just browsers. Right now, there's not a really broad use case for writing desktop apps with web assembly, but that's coming in the future. The whole reason why web assembly exists is to have a portable, memory safe, consistent environment, to write a language. And if you're thinking, Well, that sounds a lot like the old days, like where we wrote desktop apps, and then we ship them constantly.

And the idea is the same. The power of a desktop app is that it runs natively on a computer. So this is going to be my first most controversial point of the day is. No matter what you write, a JavaScript is never going to be as fast as a native desktop application.

That's because a native application written in C sharp or Java or anything like that is always going to use the low level memory instructions of the computer that it's on. So if it's using Windows or Linux or an arm system or an x86 system, a desktop application can take advantage of that because it can reach down into low level instruction code and just be more efficient.

Whereas JavaScript no matter what Always has to be interpreted first and then compiled. And so you're always going to get this idea that it's running in a sandbox environment. So it's never gonna be as fast, versus now we can write web assembly, we can get that sort of speed in a browser.

And then we can take that code and move it to a Linux machine or Windows machine and run it natively on the desktop. That's pretty cool. That's so cool. Alright, but mainly I'm trying to set context here and it's not a silver bullet, it's not gonna solve all of your performance issues or things like that.

That's not what web assembly is for. It's for this portability and the ability to take complex programs that you've already written in C, like a video encoder or decoder, and then bring them to the web and then use that power in a safe, memory efficient manner. Okay, that was a lot.

Are there any questions so far on web assembly because I talked at a super high level. And I really wanna make sure we understand what we're doing here today. And what we're going to get out of it.
>> Is a question about comparing it to Java has byte code what it compiles versus web assembly like how are they different?

>> They-
>> Are there similarities.
>> There are similarities because Java and I want to say Python also compiles down to bytecode or some form of it. There's similarities there but web assembly ultimately is going to compile down to assembly it is. It's not the lowest level of programming that would be assembly language itself.

That's as low as we can get, we can't write ones and zeros. I haven't met a human who can do that I'm sure there's like one person on the planet who can like I can code in ones and zeros. I don't know how, but the lowest level of programming we can get is assembly.

And then right above that is web assembly. So hence the name web assembly. Now Java is still going to compile to, or Python is still doing part of bytecode which has to compile down again into assembly instructions, and then it runs natively. So, in terms of performance, I would say Java is still gonna be faster, not not due to anything like input or architecture or how it's written.

It's just the more mature platform. There are more optimizations in the Pilar. There are more tips and tricks we know for optimizing all that code. Web assembly is the newest kid on the block. It's been around for a few years, but it really gained popularity once all the browser's decided that this was the thing.

We're gonna adopt it and let's make it work for everybody. And for the most part for something kind of relatively unknown in the front end land every browser supports web assembly, that's one of the reasons why I'm making this course is because we just don't really know that it's for engineers.

And if you look at some web assembly code, it's really intimidating because it doesn't look anything like programming that we know. And it makes you think like, hey, this isn't some JavaScript, this is even a language that I'm even familiar with and a syntax that I'm familiar with.

And that's why I wrote this course because as we go on in the future, we will definitely see more web assembly and I want people to be familiar with it. This is exactly what I say in my full stack course, in that you're not gonna learn everything about full stack or servers or things like that.

It's more when you see it again, you'll be familiar and if it's an area you're interested in, well, maybe I can change your career path into being a web assembly programmer. I really believe this is the forefront of the next stage of the internet, which, yeah, I'll talk more about that and my passionate and all that.

Are any other questions? I love that what about Java and bytecode. The question was, is web assembly similar to programming in assembly itself as an x86 instructions or arm or whatever micro architecture of the system they programming on? The answer is yes and no. Yes, in that the principles are the same we're actually just taking bits and we're moving them around.

We're applying low level instructions called opcodes to all of these bits. But it's a step above assembly and that we have nice things like four loops, and dealing with arrays, and if statements that aren't completely ridiculous to read, so I'd say it's somewhere in the middle. It's definitely more approachable than trying to learn assembly.

And that's really the goal of this course is to make it approachable make it understandable for everybody. But yeah, I'll do some comparisons to assembly a bit later. But for now think of it as the middle ground between assembly and a high level language.
>> I was just wondering.

So that means we can actually rethink the way we shipped like for example packages can we just swipe packages in web assembly and then they can actually. I mean, the consumer can compose those packages they could do when shipping.
>> Yes, thank you. Yeah, you're already there. You're already that's why we wanna think about this.

Yes, what we, the power we get right now is the ability to take, let's say a video decoder. It's written in C, which is a time tested, very old language. Very efficient. We can compile that to web assembly and we take all those efficiencies we got from C.

And then we run them as we run those in the browser. So yes, that's what it's designed to do is to create packages of really complex imputing and then bring that to the browser. Whereas if we try to do this in JavaScript, it would be very clunky and very onerous.

JavaScript isn't really designed to work at a low level, the way that we would need to be as optimal and as efficient as possible. You're asking about, if we just like imported the cool secret library? When we import that web assembly, is it going to be a pain to debug?

And the answer is, yes, it is, that debugging story on web assembly is probably not as strong as other languages that we have right now. Because again, this isn't as mature as even JavaScript where we have amazing debugging school skills. So, if you were at the end of this course if you say hey I have my favorite image decoder, audio processing library that I want to pull in.

You really do still have to know what you're doing because it can quickly go off the rails I'll say when we tackle memory later which I save memory till pretty far down the course because it's a really complicated topic and it's cognitively it's difficult for a lot of engineers to understand.

But you have to understand exactly what's going on. Because we're going to see sine shifting, like literally we're moving bits here and there and we're moving from a 16 bit Integer to an unsigned eight bit integer. And you really have to understand that stuff at a low level.

So I wouldn't say go out and pour your favorite library to web assembly just yet. However, there are many examples of people have done that so far and there are some links at the end where we see made with web assembly where people have ported rust and other high level programs into it.

But yes, to answer that, that's a long way to answer your question. You can. It's a little tricky though. There's the question in the chat was, can we reuse things like fetch and other browser API's or do we have to rewrite them from scratch? The answer is yes, we will use fetch.

We will use some browser API's. But the surface area is pretty minimal. And it's gonna come down to moving memory back and forth and the tooling that we're going to use later built by using assembly scripts and things like binary and things like that. But good question we will get there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now