قالب وردپرس درنا توس
Home / Tips and Tricks / What is WebAssembly and should you use it? – CloudSavvy IT

What is WebAssembly and should you use it? – CloudSavvy IT

WebAssembly logo

JavaScript is no longer the only language that browsers can run natively. WebAssembly will revolutionize the way you code for the web, making it possible to port native apps to the browser with near-native performance.

What exactly is WebAssembly?

JavaScript is a fully interpreted language. When you connect to a website, they send the JavaScript code to run. It is usually minimized (spaces and line breaks are removed to save space), but you can open the Chrome development tools and read the source code directly. Although JavaScript is quite fast these days, it still needs to be interpreted by the JS engine. Parsing can take a while and increases the overhead. In addition, JavaScript is not very efficient from the start.

WebAssembly, often abbreviated as Wasm, isn̵

7;t actually a “language” that you will hand write. It is a binary instruction format that is faster than JavaScript and very close to compiled languages. It’s still an interpreted language, but it’s meant to be interpreted by machines, not people.

Because Wasm was designed from the ground up, it was designed for performance. It removes the complex prototyping of JS and introduces the right basic elements. JS only has floating point numbers for numbers – any math operation in JS is done with floating point numbers, although floating point numbers are much slower and unnecessary in most cases. Wasm provides basic elements for ints and access to memory at the byte level. It has a text representation called WebAssembly Text (.wat) with which you can code directly. You will always package it in its binary format for use on the web. Under the hood, Wasm runs on a stack-based virtual machine that translates the instructions into actual code that runs on your machine.

Since it is just an instruction format, the magic of Wasm is that it can be used as a portable compilation target for other languages ​​without sacrificing the speed you would have while transpiling. This includes languages ​​like C ++ and Rust which can now be run on the web when compiled into Wasm.

Wasm can be used as a portable compilation target for other languages

If your mind has just been blown it’s not theoretical – it actually works in practice. AutoDesk was able to port AutoCAD, a 30-year-old C ++ code base, to a WebAssembly-based web app with relative ease in just a few months. Native apps ported to the web are a major use case for Wasm, and we will see more and more apps take this approach as Wasm pushes the capabilities of the browser.

If you want a cool demo of Wasm’s performance, you probably already have it – Google Earth runs on Wasm and renders complex 3D models with seamless LoD levels and loads as you fly around the planet – all at a perfectly smooth 60 FPS. This is simply not possible with JavaScript.

If you want to work with Wasm, you can try it out on an online WebAssembly playground.

Will Wasm replace JavaScript?

No. JavaScript still remains a fantastic language, and since NPM usage is overtaking all other language package managers, it won’t go away anytime soon.

Modern JavaScript engines like V8 are still quite fast, and while they don’t offer the same native performance as Wasm, it’s acceptable for most simple web apps and simple scripts that don’t need to do massive computing. In essence, you wouldn’t be using Wasm for basic scripting on your website. Raising an alert when a user presses a key, for example, is easy for JS, doesn’t have to be powerful enough to run a million times per second, and certainly doesn’t need to be written in C.

JS is also much more accessible, as demonstrated by the success of client-side JS frameworks like React. Web apps created with React are still quite fast and, with good render optimization, can be run on most smartphones at 60 FPS without jerks.

However, if you want to speed up and run a desktop app in a browser, Wasm is becoming the preferred format. Even so, JavaScript is required to call Wasm methods, and you will continue to use it in a Wasm code base.

Wasm on the desktop

This may seem a little uninteresting at first, but Wasm running on the server is a lot more reasonable than you think. WASI, or WebAssembly System Interface, standardizes Wasm’s interaction with the system and provides a modular environment in which WebAssembly can do all of its magic outside of the browser, either on your desktop or in a server environment. With WASI, developers can write the same app for native and web applications without running more instances of Chrome with Electron.

In a way, this replaces a lot of what Docker does. Docker founder Solomon Hykes even said so. Code written in any language (which sets this apart from .NET / JVM) can likely be compiled to Wasm and run on any server running WASI. It abstracts the code from the computer (no longer compiling for a specific CPU or operating system) and only requires WASI to be compiled and installed on the machine it is running on. While Docker isn’t going away either, it may be able to run Linux, Windows, and WASI containers side by side in the future.

This is similar to the promise made by Java: the runtime environment runs the same code on a Windows or Linux host. However, Wasm’s selling point is that you don’t have to program in Java and you are free to choose more powerful languages ​​like C ++ and Rust.

Source link