WebAssembly
Applications that run within a browser (instead of from a hard drive) are becoming more and more common. Much like office software developers like Microsoft (Microsoft 365) and Google (Docs and Sheets), which add new functions to their packages all the time, browser-based games are becoming increasingly complex and are using more resources. In many cases, these web applications are written using JavaScript. More recently, however, a growing number of developers are turning to WebAssembly – a new approach with brilliant results.
What is WebAssembly?
WebAssembly (Wasm) provides web developers with a new way of making applications available online. Previously, the only option was JavaScript. The problem is that JavaScript runs relatively slowly and performance can be a struggle in certain situations. Thus, the World Wide Web Consortium (W3C) came up with a new method – WebAssembly. In order for Wasm to function, the browser used must be able to handle the language. Mozilla (Firefox), Microsoft (Edge), Apple (Safari) and Google (Chrome) have all been involved in the development process. WebAssembly applications are compatible with all of the latest browser versions by these developers.
To experience the power of WebAssembly for yourself, try your hand at Funky Karts. The game was originally developed as a mobile app, but has been compiled to WebAssembly so that it can be played in a browser. The developer wrote an interesting blog post about the project, giving a step-by-step description of how he went about the compilation process.
Theoretically speaking, WebAssembly is represented in bytecode. You can think of it as a middle ground between machine code, which can only be understood by a computer, and a typical programming language, which is understood by humans but only after it has been compiled. This is what makes WebAssembly so fast in comparison to other languages – computers can compile the code with virtually no effort. Indeed, writing in bytecode is somewhat different. The advantage of Wasm is that you don’t have to work in this programming language yourself. In practice, developers write their applications using C or C++, for example.
The source text is then compiled using the Emscripten application. This tool existed long before WebAssembly and was used to compile C/C++ code into JavaScript (or ams.js). It can now also be used to convert code to Wasm. This means the code is pre-compiled, so that it doesn't have to be compiled or interpreted when the program is run. When a user opens the application in a browser, a small virtual machine starts up and runs the application.
Advantages of WebAssembly
WebAssembly currently only has one real disadvantage: it isn’t catching on fast enough. Web developers are used to working with JavaScript and there are no plans to replace JavaScript. The project managers have stated very clearly that they want to promote Wasm as an alternative to JavaScript. However, thanks to the support of the major browser providers and the W3C, the popularity of WebAssembly is growing. This is also helped by the fact that website visitors don’t actually have to do anything themselves – web applications using WebAssembly load just as easily as JavaScript code, only more quickly.
Developers who are already familiar with languages such as C, C++ or Rust can now write programs directly for the web. This choice of programming language also opens up more design possibilities. If you cannot find the necessary JavaScript libraries or frameworks for your program, there are other ways to make it work. Here are some of the reasons why developers should take a closer look at WebAssembly:
- W3C open web standard
- High performance and small file sizes
- Perfect for mobile browsing
- Even virtual reality programs can theoretically run in a browser
- No need to learn a new programming language
- C, C++ and Rust can now all be used to program web applications
- Supported by all of the major browser manufacturers
- No limitations for users
WebAssembly practical examples
Developers don’t actually need to write their programs in WebAssembly. One of the major advantages of the solution is that programmers can use any language such as C and later transfer their code into Wasm format. Nevertheless, it makes sense to learn more about the compiled code to uncover how WebAssembly works.
The source code is available in two versions: WebAssembly Text Format (WAT) and WebAssembly Binary Format (Wasm). The latter is the actual code a machine would run. Because Wasm is mostly binary code and difficult to understand, it is more or less unusable for human analysis. Thus, the WAT sub-form has been developed. This type of code uses readable expressions and can be examined by a programmer. However, it doesn’t provide the same programming comfort more established programming languages provide.
Let’s look at an example using a simple line of code written in C:
The same code in WAT format is significantly longer:
Although readability is diminished, certain elements can still be deciphered. WebAssembly puts everything into modules which are further classified into functions that can be specified using parameters. In the code above, we can make out five different elements:
- module: main unit in WebAssembly
- function: grouping within a module
- memory: array with bytes
- global: a value that can be used across multiple modules
- table: reference storage
Once the code is translated into binary code, it becomes unreadable:
If you’re keen to give WebAssembly a go, check out the WebAssembly Studio – an online development environment for Wasm.