Why and How Webassembly?

Why and How Webassembly?

You might have heard about webassembly by now specially if you are a web developer or javascript developer. If not here is a brief introduction to it.

Introduction

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.

Above definition of webassembly was hard to understand for me when I first read it. For simpler definition I take keywords and make my own version of definiton which is easier to understand.

Webassembly is alternative of Javascript to run your code/login inside javascript virtual machines (V8, Spidermonkey, JavascriptCore) which are fast, efficient and safe.

Let us go line by line why I choose this definition,

  1. Alternative to javascript - Javascript is programming for web specially browsers where its the only option available but sometimes javascript fails to do some high computation tasks due to limit in programming language design. Javascript engines today are fast but still there are places for improvements like running a full fledged game, graphics designing tools like Photoshop or illustrator or machine learning tasks.

  2. Target as Javascript VM - There can be other targets but why JS VMs. Today JS VMs are doing a lot of tasks beside just displaying simple interfaces. Node runs V8 engine at its core but there are a lot of steps from parsing you code, creating AST (Abstract Syntax Tree) to optimising you code depending on its implementation in different engines. What if we can remove some steps and produce optimised code or byte directly from different optimised languages like C/C++/Rust or others directly to these engines. This is what Webassembly does. Also it provides power to those users who don't work on web technologies to show there skills and work on web.

  3. Fast and Efficient - As in above point explained that webassembly creates a binary format or bytecode for Javascript VMs which is optimised. Webassembly runs on simple stack based virtual machine inside Javascript engines or Javascript VMs which in turn provides native speed by taking advantage of common hardware features.

  4. Safe - WebAssembly describes a memory-safe, sandboxed execution environment that may even be implemented inside existing JavaScript virtual machines.

Use cases

Use cases of web assembly are many cross different fields. First of all it provides better execution and toolkits for cross compiled languages to web. It provides a way to run high performance tasks like Video, Audio, Photo editing, Games, AR, VR, CAD, Streaming, Scientific simulations, Emulations, Encryption directly on browser with greater performance than its counterpart Javascript implementation.

Other features outside browser are like symmetric computation across nodes, Server-side compute of untrusted code, Hybrid native apps on mobile devices.

What its not

  1. Webassembly will not replace Javascript, it there to accompany javascript to perform better.

  2. Javscript is fast enough to do most of tasks. Moving everything to webassembly is not a good choice at all. When javascript doesn't perform as expected in a given scenario than move that code to webassembly and move that compuatation to webassembly.

  3. Its not yet feature complete like SMID need to implemented, languages which have garbage collector needs to find way to build optimised binary for webassembly.

  4. Using webasssmbly is hard as it only provide a very basic subject of data types and memory management. You need to manage memory and how javascript and webassembly will talk to each.

So, should you learn webassembly? Yes, there are benefits of learning new things, sometimes you will learn stuffs you don't know even existed or can be done or a completely new language or way to doing things.

In coming articles we will go and start building something this webassembly to know and get basic knowledge.

Sources

Happy learning, Happy Coding!