Web Assembly (Wasm)

Web Assembly & File Types

Jem Young

Jem Young

Netflix
Web Assembly (Wasm)

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

The "Web Assembly & File Types" 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 Web Assembly runs in its own environment at a near native speed. It can be cached and runs much faster than JavaScript. The two file types in Web Assembly are .wasm, which is the actual assembly code in binary format, and .wat, which is the human readable textual representation of the code.

Preview
Close

Transcript from the "Web Assembly & File Types" Lesson

[00:00:00]
>> What is Web Assembly? Like, really I've mentioned it several times, but really, what is it? Is it a language we code directly? A little bit, yeah. Is it a faster replacement for JavaScript? No, it's a compliment to JavaScript that works in tandem with it. But really what it is, is a low level language that we actually compile to.

[00:00:26]
It is meant to be compiled from Assemblyscript or Rusts, or C and all these other languages into wasm. And I'll say wasm Web Assembly, I'll use them interchangeably. If you see wasm it's short for Web Assembly, we're actually not meant to write Web Assembly directly. As we'll see later, we'll some examples of what Web Assembly looks like.

[00:00:50]
Things will get complicated very quickly. But I want you to know it because if you read the blog, you'll have to look at Web Assembly files. But what it is, is Web Assembly the secure sandbox area that runs in near native speed, almost runs at the speed of a program that is written on the desktop itself.

[00:01:11]
But it's running in the browser, which is pretty phenomenal cuz the browser actually is a pretty high level abstraction over the operating system itself. Which is a high level abstraction over axing the memory and CPU, the registers and all that. So the fact that Web Assembly can run it almost that speed of application written natively for a particular platform, is pretty impressive.

[00:01:33]
The difference in, I know this is gonna come up, and someone will ask you this, so, why Web Assembly and not a web worker. There's probably two main things. One is Web Assembly can be cached, so we can fetch our wasm file from the server. It's instantiated and then it's cached in memory, versus a worker which has to be instantiated every single time.

[00:01:57]
And as you know, Web Worker is a way of spinning up a separate thread away from the UI thread in JavaScript. It's generally how we were doing heavy computation before. And there are pros and cons to a worker versus Web Assembly. But at the end of the day, remember Web Assembly isn't necessarily written for JavaScript engineers to make our lives easier, it's written for other types of languages that compile down into Web Assembly.

[00:02:23]
And that to me is one of the bigger, I think points of contention, and points that people miss about what Web Assembly is, they're like, why wouldn't you use a Web Worker, you can offload. You can do a lot of the same things in Web Worker. You can do it in Web Assembly, well, not same things, but you can perform heavy calculations.

[00:02:39]
You don't block the UI thread. You can pass information back and forth. Yeah, there's definitely use case for workers. But Web Assembly isn't designed to compete with workers. It's designed for other languages compiled down and we have this shared platform. Is there nods is that everybody understands that ,because I think that's the biggest thing of like, what is Web Assembly for?

[00:03:02]
What are we using it for? Why not use these other features? It's primarily designed to run in other languages. Okay, any questions on that? All right, cool. So this also confused me when I was first looking into, what is Web Assembly? So if you can't tell, I designed this course around all of the questions that I had when I was learning these things, and I write them down.

[00:03:30]
So that when you run in the same questions, you hopefully won't have the same problems I have. When I saw that Web Assembly had two different types of files. I was like, what? What, did it? Nobody, man, my jokes aren't landing today, all right. So, Web Assembly there is the wasm file.

[00:03:53]
The .wasm file is the actual assembly code, written in binary format, that the machine will run. It is not readable by humans. It's just a bunch of hexs and zeros, and things like that. We're never gonna access it directly, we're never gonna read it directly. Fortunately, the good people who created Web Assembly and this giant committee of people.

[00:04:20]
They're like, well, it turns out people wana debug their program, so we can make them debug actual assembly code like actual binary code. Or we create another file that goes alongside it. And that's the wat file, the wat file is a text format, it's readable by humans. We only use it for editing and debugging.

[00:04:42]
But at the end of day, it's still gonna get compiled to a wasm file, so you can edit your wat file all day and say you compile it back to wasm, it doesn't really do anything. It's interesting that it is a format that, the wat file, that you're coding but you're not actually coding anything, until it's compiled the machine can't run it at all.

[00:05:03]
It's purely for us humans who can't read binary. And, so all the files today, when we talk about, when we write some Web Assembly in a bit. And when we see Web Assembly and we see examples of it, it's always in the wat format. It's not in wasm format.

[00:05:18]
The wasm was what we're gonna import into our browser later. Okay, I want to clear it up, cuz it does. When you look at Web Assembly it does seem confusing, and it's a weird, I don't know if there's other languages that have that concept of. There's two different formats, like the actual code being run and the actual code that you're writing.

[00:05:38]
And there are two different files that you pull together or that are sent down together. So it's a little tricky, okay. Also wat, is just funny, it's a funny name for file format.

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