Introduction to Web Assembly

What is Web Assembly, and what is it for?

Alexander Curtis
3 min readMar 6, 2019
Photo by Alexandru-Bogdan Ghita on Unsplash

Before Web Assembly

For over twenty years, the only programming language universally understood by web browsers was Javascript. Anyone wanting to program for the browser either had to write in Javascript or else use a tool to convert (compile) their preferred language into Javascript.

Either way, a Javascript interpreter (runtime) needs to examine the running program in order to map Javascript's syntax and abstractions to the memory and processor of the underlying hardware. This constant process of interpreting and compiling means that any software running in a web browser will run more slowly than if it were written specifically for the machine on which the browser itself runs.

The rise of Web Assembly

The Firefox web browser shipped support for Web Assembly in 2017, and the other browser vendors quickly followed, so that in 2019 all the major browsers now support it.

The rise of Web Assembly marks the start of a new era in web application development, where the browser becomes a platform for running applications at a speed close to that of native software, and with support for any programming language, not just Javascript.

What is Web Assembly?

Web Assembly is a standard that defines a limited set of operations, together with a model for handling memory, that maps closely to hardware platforms like PCs, Macs and mobile phones. It is designed to close the gap between the web browser and native applications. Rather than the browser converting a high-level language into machine code while the program is running, the developer compiles their program into Web Assembly before it is shipped. The browser still has to convert the Web Assembly into native code for the host machine, but this is much easier since the platforms are so similar.

Javascript is still needed, for the time being, to load and run the Web Assembly program, but there is nothing of Javascript in Web Assembly itself; any language can be compiled into Web Assembly and run in a web browser. In fact, compilers have already been written for C, C++ and Rust among others.

Programming in Web Assembly

To those used to high-level languages like Javascript or Python, the range of instructions provided by Web Assembly seems very restricted. There are instructions to add two numbers, to subtract, multiply and divide; instructions to copy values in memory; instructions to perform Boolean logic, and instructions to control the path of execution, but that’s all. Everything needs to be done using just these instructions. Looking for Math.sin? You have to write your own algorithm to calculate the sine of a number. Concatenate two strings? Web Assembly doesn’t even know what a string is — it only understands numbers.

All this might seem crazy, but it’s because Web Assembly is not designed to be written by humans. It’s designed as a compilation target for other languages, and the compilation environments for those languages will provide whatever maths, string and other algorithms the language needs.

That said, look out for my next post in which, rather than compiling a program from a high-level language into Web Assembly, I code the program in Web Assembly’s own low-level assembly language.

--

--