What is WebAssembly?

Lawrence Hon
The Wonderful World of Code
4 min readJun 28, 2018

--

To understand what WebAssembly (Wasm) is we should first understand the differences between compiled languages like C++ or Java and interpreted languages like Javascript, Ruby or Python. When someone writes a program in a compiled language the code must first be translated into another version of code so that the computer can understand it. This is done with a piece of software called a compiler. Only after it is translated can it be run and do the things that the programmer intended.

Interpreted languages, on the other hand, do not go through this process. Rather, when a programmer runs the program, the computer automatically does the translation and the program begins to run.

So what does this have to do with WebAssembly?

JavaScript is an interpreted language and so when a website or web app is written, the browser will look at the file, line by line, and interpret the code (via the Javascript Engine). JavaScript is the de facto language of the web. For better or worse, if you want to do web development, you will come across JavaScript. As amazing as Javascript is, however, compiled languages offer a new world of possibilities in terms of performance and security. CPU intensive applications are better suited to languages that can handle multithreaded processes. As it stands, certain applications cannot be developed for the web.

Enter WebAssembly

WebAssembly is a new binary format that can compile C, C++ and Rust into the browser. This essentially means that web apps can be written in languages other than JavaScript. An application written in C++ can be compiled by WebAssembly and run within the JavaScript engine of the browser.

The implications for WebAssembly are numerous. Developers hope WebAssembly can be the sole compilation target for languages. This opens the space for any language to be used in writing applications for the web. Because decoding binary formats is faster than parsing JavaScript, loading can be much faster for web applications (on both mobile and non-mobile platforms) in general but especially for CPU intensive applications. Games, scientific computations and other applications that require enormous amounts of resources can be run on the browser. Because most of the processing has been completed during the compilation phase, energy consumption should be drastically reduced; something that benefits mobile platforms.

WebAssembly also has implications for a more robust security infrastructure due to the way it handles memory. WebAssembly has an execution stack separate from the program and so variables and functions are not able to be changed.

In addition to improved security, WebAssembly can drastically improve the load and execution times of web applications. JavaScript engines currently load all .js files individually and parsed line by line. The JavaScript engine then tries to optimize certain processes or divert more resources to hot code (code that requires more CPU processing power) sections. After optimization, the engine then converts this to machine code. WebAssembly skips this optimization step during runtime because it has been previously optimized during compile time. As a result WebAssembly converts code straight to machine code.

Summary

  • Faster load times due to loading precompiled files
  • Potential lower energy usage
  • CPU intensive applications available over the web
  • Increased security

Impact on JavaScript

WebAssembly functionality is designed to run alongside JavaScript, not replace it. Javascript is very good at certain tasks that compiler languages are not optimized for. For example, JavaScript is able to manipulate the DOM which allows for some of the core functionalities of web applications. JavaScript makes clicking a button or filling out forms and sending that data to the backend effortless. Imagine implementing that with C++!

JavaScript also has the ability to access platform specific Web APIs (like console.log(), AJAX, the DOM, setTimout) which opens the door on web app functionalities. WebAssembly does not currently have access to Web APIs. Although this may change in the future JavaScript is still the best way to call these APIs.

Rather than replacing JavaScript as the de facto language of the web, WebAssembly opens the doors for other languages to expand the functionality of the web.

Resources

--

--