Mats Bryntse
1 November 2022

WebAssembly and the Future of Frontend Development

JavaScript has been the lingua franca of web browsers and web-based applications for over two decades. No other language even […]

JavaScript has been the lingua franca of web browsers and web-based applications for over two decades. No other language even comes close to replacing it. In addition, over the years, JavaScript has improved, and the overall experience for both developers and users has improved as well.

JavaScript makes it easy to build robust web apps with all its modern features. Moreover, its community is one of the largest and most active among all programming languages.

Despite its popularity, it’s far from perfect. For instance, JavaScript as a dynamically typed language can be slow, especially for CPU-intensive tasks.

WebAssembly, or Wasm for short, is a new web technology that’s growing fast and gaining popularity. It allows you to import compiled bytecode into your application via JavaScript and run it in the browser. It’s designed to run alongside normal JavaScript client code and can unlock features that are beneficial for high-performance browser applications, like video editing, image processing, data visualization, virtual reality, and video games.

In this guide, you’ll learn about WebAssembly, how it works, and how it may change the web landscape.

What WebAssembly Is and Why You Need It

While JavaScript is an excellent tool for the web, it wasn’t really designed to handle large and complex applications.

WebAssembly, in contrast, is a new type of binary code that allows you to develop apps in multiple languages, compile them down to low-level code, and run them in modern web browsers with near-native speed and performance. It’s compact to download and efficient to compile and execute.

Wasm was originally created in 2017 to run high-performance browser-based applications without affecting compatibility or security. Currently, Wasm provides significant performance improvements in two main areas. The first is start-up speed, allowing even computing-intensive applications to load quickly. The second is that you’ll notice a much smoother application performance while the software is running.

With Wasm, programmers can bring their native C, C++, Rust, or other supported language applications to the web. This enables them to achieve full performance and take advantage of the complete set of capabilities that they have when running natively on Windows or Mac. This is revolutionary because it means that users can now simply go to a web page and start using the app without downloading it, installing it, or running any security risk.

How WebAssembly Works

On December 5, 2019, WebAssembly became the fourth language standard to run natively in browsers, along with HTML, CSS, and JavaScript. Since then, there’s been some confusion about what WebAssembly is, what it can and cannot do, and how to use it.

WebAssembly architectural diagram

WebAssembly files come in two different formats. A WASM file, which is executable by WebAssembly’s virtual machine and readable by web browsers, and a WAT file, which is in text format and can be read by humans.

To produce a Wasm application that your browser can read and execute requires the following steps:

When you follow these steps, you’ll have a web application that is written in another language but can run in your browser.

There are several tools available, like Emscripten or wasm-pack, that can help you build a WebAssembly application by converting your program to Wasm.

It’s critical to note that WebAssembly is intended not to replace JavaScript but rather to complement it. It was initially created to be a tool that would work around JavaScript’s limitations.

WebAssembly’s Benefits

As previously mentioned, one of Wasm’s major benefits is that it’s suitable for CPU-intensive tasks. Because JavaScript is single-threaded, it only has one call stack and memory heap, and it is not the most optimal option for tasks that require a lot of CPU. WebAssembly is written in lower-level languages like C, C++, or Rust, which means it doesn’t require much CPU time to compute.

In addition, WebAssembly is in binary format, which means it requires fewer bytes to download, resulting in a faster start-up time. In contrast, JavaScript is text-based (rather than binary-based), which means it requires a large number of bytes to download and, therefore, requires a longer start-up time.

Another JavaScript limitation is that JavaScript interpretation and just-in-time (JIT) compilation consumes a significant amount of CPU. By using a JIT compiler, parts of the code are removed at run-time and placed in the compiler, where they are then interpreted while the program is running.

This JIT compilation comes at a significant performance cost, as each instruction incurs some computational overhead in order to be converted to an instruction that the virtual machine running the JavaScript understands. In a JIT environment, you have to download the code each time it’s loaded, tokenize it at run-time, parse it into an abstract syntax tree, and then execute the code from that tree each time the JavaScript is loaded. This is extremely heavy and causes the code to run slower than natively compiled languages.

WebAssembly generates a binary file that is decoded and compiled to machine code on the web without the need for any further optimization. This makes it superfast, and it consumes less CPU. It can use either JIT or ahead-of-time (AOT) compilation, and compiling language ahead of time means that users can expect faster load times.

Finally, with Wasm, all your legacy code written in various programming languages can be used as is. There is no need to rewrite non-JavaScript libraries, modules, or apps into JavaScript in order for them to work on the web, saving developers hours of unnecessary work.

WebAssembly’s Limitations

WebAssembly is a new technology that’s constantly improving. As with every new technology, it has limitations that must be resolved moving forward.

For instance, Wasm can’t interact with the DOM directly without JavaScript. This limitation prevents users from creating stand-alone applications.

WebAssembly also has some security concerns that attackers can exploit. For example, when a Wasm engine isn’t implemented securely, a malicious developer can violate the security properties of the browser and compromise it. It’s also possible to compromise a Wasm application itself if it isn’t written securely.

In addition, one of WebAssembly’s limitations is the lack of integration with garbage collectors. Since the garbage collector is in charge of memory release and allocation, this absence means that developers must manage memory manually, which can result in memory leaks. Of course, web browsers are getting closer to providing support for garbage collection, making WebAssembly easier to use with languages like Java or C#.

What WebAssembly Means for the Future of Frontend Development

It’s possible that WebAssembly could play a big role in the future of web development; however, JavaScript is not going away anytime soon. Even if WebAssembly becomes more popular and gains a more significant part of the web development sector, there are so many businesses and so much stuff built with JavaScript that it’ll be a long time before anything changes.

Furthermore, as JavaScript continues to improve, it becomes increasingly attractive for frontend and backend development that doesn’t involve processing-heavy features. If a project requires advanced performance, Wasm is here to help.

The consensus is that JavaScript, HTML, and CSS should be the default technologies for your web pages. Modern JavaScript engines are fast and optimize your JavaScript code extensively.

Using WebAssembly for simple tasks won’t improve the performance of your application; however, you should use it when dealing with CPU-intensive tasks, such as image and video manipulation, or when dealing with applications with complex business logic that expect high performance and require your code to be as efficient as possible.

Conclusion

WebAssembly is an exciting new technology that significantly changes the web development landscape. It’s difficult to predict how the change will unfold, but as the technology keeps improving, it’s clear that the future of web development is bright.

Wasm offers high performance and the flexibility to build web applications with multiple programming languages other than JavaScript. This doesn’t mean that WebAssembly will replace JavaScript anytime soon, as Wasm was created to work alongside JavaScript to improve web performance, and most developers will use both technologies for their projects.

Are you already using Wasm today? We would love to hear about your experience with it.

This article was written in cooperation with Alex Doukas. Bryntum helps the world stay on schedule. Our component library offers a selection of high-quality, advanced UI controls for various frontend frameworks. Want to learn more? Try our scheduling and Gantt components at bryntum.com.

Mats Bryntse

Development Tips 'n tricks