www.apress.com

19/12/16

Empowering Your Web Applications with WebAssembly

by Mariot Tsitoara

There is no doubt that JavaScript and other popular web-based programming languages like Python, Java, C++, C# have made a significant impact on the web landscape. While new programming languages come and go as we progress through the digital age, it’s also fair to say that many of the popular ones are here to stay. Let’s consider JavaScript for a moment. There are currently an estimated 1.7 billion websites online right now, and around 95% of them used JavaScript as a client-side programming language in some form. JavaScript’s place in internet history can’t be understated, but at the same time, it’s not a perfect language. This is where WebAssembly comes in.
 

What Is WebAssembly?

Put simply, WebAssembly is an open standard that defines a portable binary code format for executable programs. It essentially allows you to write code in other programming languages like Rust, C/C++, GO, and others; and compile them to run on the browser. Its power lies in its ability to execute code at native speed, allowing for an extremely fast and efficient experience. With WebAssembly, you get to experience the advantages of your favorite programming languages while benefiting from the power and performance of the Web, and all without having to learn a new language.

It can now be run in most modern browsers including Chrome, Safari, Firefox, and Edge.
 

What Is The Purpose Of WebAssembly?

The purpose of WebAssembly is to empower web applications through better speed, performance, and optimization. Every browser interprets and executes code through a JavaScript engine. While a JavaScript engine may be perfectly suitable in a lot of instances, it isn’t ideal in all situations, and this is where WebAssembly offers a significant advantage. The low-level binary format allows for smaller app bundles, essentially meaning it’s smaller in size, so faster to execute. It’s also statically typed and uses linear memory, both adding to its increased speed.

There’s a thriving community of developers putting WebAssembly to use today. For example, there’s a fun browser game called Funky Karts written in WebAssembly, a WebAssembly pathfinding demo, a 2D character animation program, and much more. It is a great choice when it comes to games. Let’s say you have a  casual game that players want to hop onto when they have a few minutes of downtime. Casual games are there to provide fast and light entertainment, so users must be able to start the game straight away. Due to its power and speed, WebAssembly lends itself well to this task, getting games started quickly. It also works well for AAA games that have heavy assets that historically may suffer from speed problems.

However, it’s not just in quirky games and image editing that it thrives. It’s not a program with a niche set of applications, but rather a new way of doing things that has the potential to shift every element of the web experience. Inside the browser, WebAssembly can be used to provide better performance for CAD applications, music applications, on interactive education software, with VPNs, with live video augmentations, and the list goes on.
 

How WebAssembly Will Empower Your Web Applications

We live in the digital age where we expect faster and faster responses from our web applications. Gone are the days that you would wait patiently for a low-resolution image to load section by section, all the while hoping no one jumps on the phone thereby cutting your connection. We now have increasingly powerful, dynamic, and almost futuristic web applications that we couldn’t contemplate even a decade ago. Of course, this explosion in powerful web apps all ties into the data boom we have been experiencing in the last 5 years. It’s now easier than ever to capture and analyze large chunks of data that we can use to fuel powerful web and AI programs.
 

What Are The Implications Of WebAssembly?

When discussing what WebAssembly is and it’s potential to empower web applications, it’s also important to discuss what it isn’t. It isn’t intended to be a replacement for JavaScript or any other web programming language. Its goal is to bring more power to JavaScript applications so they can perform at a higher level and you can get the most out of your applications. With WebAssembly, you aren’t forced into choosing a new programming language, you can use JavaScript and WebAssembly in conjunction to empower your JS app. There are many areas where JavaScript struggles to perform in the ways that modern web applications and their users demand, and it’s in these situations that we can assign WebAssembly to resolve the problem.

WebAssembly does all the heavy lifting in the browser so JavaScript can do what it does best. For example, you can build in high levels of expressiveness into your apps with JavaScript, but then have them run at extremely fast speeds using WebAssembly. Certain large and powerful applications will send huge numbers of calls to the server to do heavy calculations. Not only does this affect speed and performance, but it just doesn’t make for good optimization when WebAssembly can do it better. It optimizes the process so that the heavy calculations are handled directly on the browser, making your application faster and safer.

So what about the future? Is WebAssembly the future? There has been a lot of buzz surrounding it over the last couple of years leading to more and more developers taking notice. And when developers take notice, they start developing! That’s where WebAssembly is at right now. There’s been a huge increase in use cases for it and the list continues to grow.

Speed is undoubtedly an important factor when it comes to any application or technology, but it’s especially true when considering larger, more technical, and more powerful applications. However, it’s not ALL about speed, but also flexibility, functionality, and optimization. 

This is where WebAssembly really sets itself apart from other speed-boosting projects of the past. One huge perk of WebAssembly is that it expands the types of programs that can be delivered on the web, essentially allowing for a more diverse and robust user experience and web landscape.


About the Author

Mariot Tsitoara is a Python and JavaScript developer with a passion for the Open Web. He has been involved with Mozilla as a Rep and a Tech Speaker since 2015 and has spoken extensively about Open Source and new technology, including Rust, WebVR and online privacy. You can find him on Twitter @mariot_tsitoara.

This article was contributed by Mariot Tsitoara, author of Beginning Git and GitHub.