WebAssembly

What is WebAssembly?

Providing new features and major gains in performance, WebAssembly is a new binary format which allows code to be directly executed by modern browsers. So, now you can code in high level languages which can then be compiled to WebAssembly module (wasm) and can be ran directly in your browsers. You can also call it a bytecode that can run on web browsers.

As you know, for a long time JavaScript was the only language available for developing web apps as there was no other language that could run inside the browser. But it has a lot of disadvantages such as it’s slow and has type safety issues. To be honest, JS was never designed to be used for heavy computation apps. To overcome some of the drawbacks of the language, many people started building transpilers to convert other languages (such as TypeScript etc) to JS. But the final code running was still JS. Promising near-native performance for web apps, WebAssembly was created to sort out limitations of JavaScript and it is proving to be a revolutionary step towards the future of web development.

One other thing, WebAssembly is not only useful for running modules inside the browser but outside of it too. Now, you’ll be thinking why I would want to put the code into a WebAssembly module before executing it, since, I can already run C++ or Rust code outside of the browser by just executing the program from the command line. Well, I can give you plenty of reasons why you should do that but let me give you just one here and that is security. WebAssembly modules have well-defined semantics for what memory they can access. WebAssembly could provide more reliable sandboxing for untrusted code.

What WebAssembly is not?

Well, it isn’t a programming language on its own, as mentioned above you code in high level languages (such as Rust or C++) and then compile it into binary .wasm file.

It’s not a replacement for JS. In fact, it is supposed to be run alongside JS. For instance, one may choose to build only high computation code in WebAssembly and use it alongside with other light weight JS code. Also, JS is needed to load wasm modules in browser.

WebAssembly Architecture

It is designed to run on stack-based VM. In a stack machine, most of the instructions assume that the operands are sitting on the stack, which is quite different than a register machine where the operands are stored in CPU registers and computations happen there itself. You’ll better understand with the help of an example, take the following instructions:

push X

push Y

subtract

To subtract two numbers in a stack machine, you push them onto the top of the stack. Then you push the “-” operator onto the stack. The two operands and the operator are then popped off the top and the result of the subtraction is pushed on in their place. There are a number of well-known stack machines like JVM, .NET Runtime and so on.

WebAssembly doesn’t have a heap in the traditional sense. There’s no concept of a new operator. There’s also no garbage collection. Instead, it has linear memory i.e. memory is represented as a contiguous range of untyped bytes. Your WebAssembly module can grow the linear memory block in increments called pages of 64KB if it needs more space.

One of its most significant design constraint is that it has support for just four data types (all numeric): i32, i64, f32, f64 for 32 & 64-bit integers & floating point numbers

Advantages of WebAssembly

  • Speed: Wasm is believed to be only around 10% slower than native machine code which is astonishing.
  • Effectivity: Binary format, fast parsing, and compilation. All heavyweight operation will be hidden in WASM module
  • Security: sandbox model of execution
  • Offers a good compile target for the web, so that people can choose whatever language they want to code their websites into.
  • The code, inside of the module can be debugged natively from the browser console.
  • Don’t break the web — WebAssembly is designed so that it plays nicely with other web technologies and maintains backwards compatibility.

Is WebAssembly the future of the Web?

Well, one thing that must be going around in your mind right now is why someone would want to ditch a language as simple as JS and start coding in C/Rust, which are some of the hardest languages to learn. Well, WebAssembly is not that old, it lacks a strong community at the moment. But think of all the high-performance video and photo transcoding libraries, any graphics or physics engine that uses OpenGL etc. These will likely be made available via WebAssembly modules once its community starts growing in size. WebAssembly can be revolutionary for the game development companies!

Reshaping what is possible to do in the web browser, the heavy performance gains that WebAssembly provides in the code execution could one day see the heaviest of desktop software running inside the web browser. This may ultimately lead to the extinction of native desktop softwares one day.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.