Introduction to web assembly

Tobias
#javascript  #wasm 

Web Assembly is the latest official support language from the W3C. So we are going to introduce this new language and discuss if it could become a real threat to the almighty JavaScript in 2020 😱.

Logo

Official support from W3C

The World Wide Web Consortium (W3C) announced on December the 5th in 2019, that the WebAssembly Core Specification is now an official web standard. This makes WebAssembly the fourth language for the web, following HTML, CSS, and JavaScript. The W3C comment this decision as followed:

The arrival of WebAssembly expands the range of applications that can be achieved by simply using Open Web Platform technologies. In a world where machine learning and Artificial Intelligence become more and more common, it is important to enable high performance applications on the Web, without compromising the safety of the users. - declared Philippe Le Hégaret, W3C Project Lead.

What is web assembly?

According to the official website Wasm is definded as followed:

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

What does this mean?

Developers don’t write WebAssembly personal, they write the program in their preferred language like C, C++, Go or Rust. This is code is then compiled into WebAssembly bytecode. The bytecode runs then on the client in a web browser where it is translated into native machine code and executed at high speed. Which JavaScript could never reach at the moment.

What are the benefits of web assembly?

It brings better performance to the web. Especially for loading times, and heavy math stuff like games or 3D modelling. It offers a good compile target for the web, so that people can choose which language they code their websites in.

  • Better compile target than JavaScript because it is not a dynamic interpreted language (Sure, JS should never be the compile target).
  • Software developers or non-frontend developers developers don’t have to write JavaScript to get their app on the web.
  • Files can be a little smaller to download since it is a binary format.
  • Wasm is faster to parse and compile because it is much closer to machine code.
  • It can be substantially faster to run, again because it’s close to machine code, and because it has static types and is generally designed for speed.

Why should it be a threat for the almighty JavaScript?

WebAssembly code is fed into the JavaScript execution environment. It gets the same sandbox and the same runtime environment. Also, WebAssembly is compiled in a way that makes further efficiencies possible. Nowadays these efficiencies are more dramatic than before, and the browser can skip the JavaScript parsing stage altogether. For an ordinary bit of logic (say, a time-consuming calculation), WebAssembly is far faster than regular JavaScript and nearly as fast as natively compiled code. That’s advantageous if you have a game or application which should run in the browsers.

WebAssembly can even do more than that: It opens a narrow pathway for framework developers to squeeze their platforms into the JavaScript environment. It can’t sidestep JavaScript, because it’s locked into the JavaScript runtime environment. In fact, WebAssembly needs to run alongside at least some ordinary JavaScript code, because it doesn’t have direct access to the page (yet - we don’t know what the future will bring). That means it can’t manipulate the DOM or receive events without going through a layer of JavaScript. But there are already projects and experiments which try to achieve that.

You can use WebAssembly and JavaScript together as both languages can complement each other. WebAssembly can be run alongside JS using WASM JavaScript APIs. It will load the WASM modules into a JS app and share the functionalities together. When used together, developers get the benefit of expressiveness and flexibility of JavaScript and performance and power of WebAssembly. Therefore, Wasm has the ability to make JavaScript even greater.


Tobias

Tobias is Lead Web Developer at karriere.at and programmer with heart and soul, he loves to bury himself in complex problems and to master them with creative solutions. Frontend, Backend doesn’t matter, he enjoys every task.
If there is no improvement for a web application on the agenda, he likes to learn more about artificial intelligence and machine learning.


We're a team of makers, thinkers, organisers and digital explorers and we're always on the lookout for talented people.

Are you a good fit?