in

WebAssembly for Beginners – Part 1: An Introduction To WASM

WebAssembly code on a screen

Hi there! Have you ever thought about running web apps at native-like performance? If so, you have an exciting option: WebAssembly, also known as WASM.

As a fellow tech enthusiast, I‘m sure you‘ve noticed that current web technologies focus heavily on functionality over performance. JavaScript lets us manipulate the DOM, draw graphs, and even build games – but it‘s not the fastest at pure computation.

In this guide, we‘ll take a deep dive into WebAssembly. By the end, you‘ll understand what WASM is, what it can do, and how it might shape the future of web development. Let‘s get started!

What is WebAssembly?

WebAssembly is a new low-level programming language that runs in modern web browsers. It uses a compact binary format that allows it to execute at near-native speeds.

I like to think of WebAssembly as a kind of "Assembly language for the web." It‘s designed to match the performance of languages like Rust, C++, and C – which give native performance since they talk directly to the hardware.

The key innovation of WebAssembly is compiling code from C/C++/Rust into a portable low-level format. This lets us run code at near-native speeds, even inside a web browser!

To understand WebAssembly, we have to start with JavaScript.

JavaScript is THE language of the web – it‘s used in every modern web application. But as developers, we know JavaScript can be slow compared to native code. That‘s because JS is dynamically typed, meaning extra type checks happen at runtime. The V8 interpreter must convert JS to machine code dynamically before it can even be executed:

V8 Engine in Web Browser
V8 Engine in Web Browser

By compiling C/C++/Rust down to WebAssembly, we skip all that overhead. Code is validated once ahead of time, then executed at full speed by the browser.

This is a game-changer. With WebAssembly in V8, we can now run high-performance apps on the web. Developers can write performance-critical sections in C/C++/Rust and seamlessly integrate them into JavaScript web apps.

WebAssembly interaction with JS, CSS, and HTML
WebAssembly interaction with JS, CSS, and HTML

For example, you could use WebAssembly to run the FFmpeg video encoder inside a web app. FFmpeg is a complex C app that was previously impossible to port to the web. But using Emscripten and WebAssembly, you can now encode videos directly in the browser with ffmpeg.wasm!

Key Benefits of WebAssembly

Now that we know what WebAssembly is, let‘s look at some of its killer features:

Near-Native Performance

WebAssembly executes at speeds close to native machine code. It uses the underlying hardware efficiently with minimal overhead.

Portability

WASM code can run identically across platforms and devices. It works on Windows, Mac, Linux, smartphones, etc.

Size Efficiency

The binary format used by WebAssembly is extremely size-efficient, keeping payloads small. This improves load times.

Security

WebAssembly executes inside the browser‘s sandbox environment just like JavaScript. This contains it away from the underlying hardware.

Debuggable

Despite being a low-level language, WebAssembly provides a text format for analysis and debugging. Developers can view and step through WASM code.

Streaming Compilation

The browser compiles WASM in a streaming manner as code is downloaded. There‘s no need to wait for the full payload – execution begins after the first byte.

JavaScript Interoperability

WebAssembly works seamlessly with JavaScript. You can call WASM functions from JS and vice versa.

Together, these attributes make WebAssembly a game-changing tech for the web. It unlocks performance levels not previously possible while retaining the flexibility of JavaScript.

WebAssembly vs JavaScript

An important question developers have is: will WebAssembly replace JavaScript?

In my opinion, the answer is no – at least not completely.

WebAssembly is designed to work within the broader web ecosystem. The goal is performance, not outright replacement. WebAssembly excels at computationally intensive tasks like:

  • Encoding/decoding media
  • Computer vision
  • Physics simulations
  • 3D rendering
  • Data analysis
  • Math-heavy computations

But it has no ability to manipulate the DOM or do general application logic. This is why JavaScript interoperability is so crucial.

In practice, WebAssembly can accelerate critical paths in a JavaScript web app. You identify performance bottlenecks and optimize them with WASM while retaining the outer JS application shell.

This division of labor keeps the web a vibrant ecosystem. JavaScript handles app logic, event handling, and UI. WebAssembly accelerates the number-crunching internals.

Current Adoption Status

WebAssembly is still quite new, but it‘s gaining traction rapidly.

All major browsers now support it, including:

  • Chrome
  • Firefox
  • Safari
  • Edge

Browser adoption reached ~67% globally by the end of 2019, just two years after initial release:

Year % Browser Support
2017 1.1%
2018 4.5%
2019 67.0%
WebAssembly browser support over time

Source: StatCounter

On the backend, WebAssembly works in Node.js, Deno, and cloud platforms like AWS Lambda.

Popular JavaScript frameworks are also adding WebAssembly support, like React via projects like react-native-webassembly.

So while adoption is still ramping up, all signs point to WebAssembly becoming ubiquitous across the web stack.

Limitations of WebAssembly

Of course, WebAssembly isn‘t perfect. Some current limitations include:

Lack of Garbage Collection

WebAssembly doesn‘t provide automatic memory management like JavaScript‘s garbage collector. Developers must manually allocate/deallocate memory.

Browser Compatibility

Although all major browsers support WASM, there are still compatibility issues. Code may need tweaking to work across browsers.

Debugging Difficulties

Debugging WebAssembly code can be challenging compared to JavaScript. The low-level nature makes debugging complex.

Security Concerns

Since WebAssembly code is opaque, malicious code could be slipped in. Browsers can‘t easily scan WASM code like they can JavaScript.

No DOM Access

WebAssembly can‘t directly access the DOM or other web platform APIs. JavaScript glue code is required.

Steep Learning Curve

Developers must learn C/C++/Rust to use WebAssembly. This raises the barrier to entry.

These issues make WebAssembly imperfect for some use cases today. However, I believe they will smooth out over time as tooling and practices mature.

The Road Ahead

WebAssembly is still evolving rapidly.

The W3C and WebAssembly Community Group are working to improve WASM and push its adoption. A big focus is tackling compatibility issues across browsers and platforms.

Longer term, there is interest in expanding WebAssembly‘s capabilities, for example:

  • Allowing DOM access without JavaScript glue code
  • Adding threads and concurrency
  • Improving debugging/profiling tools
  • Expanding language support beyond C/C++/Rust

I encourage you to check out the official WebAssembly roadmap to learn more about what‘s coming.

Closing Thoughts

WebAssembly is a game-changer that unlocks near-native application performance on the web. Although it‘s still evolving, WebAssembly provides immense value today by accelerating JavaScript apps.

If you‘re looking to push web application performance to the next level, I highly recommend giving WebAssembly a try. Reach out if you have any other questions!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.