XenonStack Recommends

DevOps

WebAssembly vs JavaScript | The Ultimate Guide

Navdeep Singh Gill | 13 October 2022

WebAssembly vs JavaScript | The Ultimate Guide

Introduction to Wasm

Wasm, officially called Web-Assembly, is an Optimized code format and API for the web. It can significantly enhance the performance of websites. Its version 1.0 was released in 2017 and has become an official W3C standard. Its support has quickly spread to all the major browser systems for use cases like in-browser: video editing, 3D games, VR and augmented reality, P2P services, simulations, and more.
Wasm is not only limited to use with browsers but is being used in mobile and edge-based environments for performance enhancements.

An application programming interface API Testing attempts to connect an application to the web and different APIs. Click to explore about, API Testing Tools and Best Practises

A wasm compiled file with extension .wasm contains low-level binary instructions (bytecode). This is executable at nearly the same speed as native instructions execution speeds (near CPU-native speed) by a VM that uses a similar or common stack. The compiled code is packed into objects or modules, directly executable by the browser with multi-instantiations per module. All the functions defined inside the modules are listed in a dedicated array or table, and corresponding data is stored in an array buffer. The VMs inside modern browsers supports both JavaScript and WebAssembly in the following described way:

  • The browser downloads a web page written in HTML and renders it if the HTML calls JavaScript code, which is then executed by the browser's virtual machine.
  • But, if the JavaScript code contains an instance of a WebAssembly module, it is fetched as described above and then used as required by JavaScript via the WebAssembly APIs.
  • When the WebAssembly code produces something that would modify the DOM – the structure of the "host" web page – the JavaScript code receives it and proceeds to the actual alteration.

Why is Benchmarking important?

To begin with, because wasm files are binary instructions, they can be substantially smaller and thus download a lot faster than JavaScript files with comparable functionality. Above all, before a browser can transform Javascript files to bytecode accessible by its internal virtual machine, they must be thoroughly processed and verified.

  • Instead,.wasm files can be checked and assembled in a single pass, allowing for "Streaming Compilation": a browser can begin compiling and executing them as soon as it downloads them, precisely like streaming movies.
  • However, not all WebAssembly applications would be faster – or smaller – than identical JavaScript applications that experts have painstakingly optimised.
  • Points like these make it a very interesting topic for having a practical example to see which one is better and by how much.
  • There has been a lot of benchmarking done by various parties in this regard, and have produced some remarkable results.
  • There has been a lot of benchmarking to evaluate how well wasm works. Consider the benchmarking of the K-means clustering algorithm.
  • k-means clustering is a vector quantization method that seeks to partition n observations into k clusters. Each observation belongs to the cluster with the nearest mean (cluster centres or cluster centroid), which serves as the cluster's prototype.
  • As evident, with an increase in the k value (iterations), the performance of wasm proves to be better (4X than the JS counterpart).
API proxies decouple the app-facing API from your backend services, shielding those apps from backend code changes. Click to explore about, Understanding APIs and API proxies

What is the benchmarking comparison between JavaScript and WebAssembly?

So, to have a benchmark between JavaScript and WebAssembly on performance metrics. Let us build a simple application.

Main points to keep in mind :

  • Keep it simple
  • Use as many standard modules as possible to make it equivalent.
  • Have similar implementations
  • Benchmark on the same criteria

So, we begin by choosing an Algorithm to use for computation in our application. I have decided on the Quicksort algorithm.

Here is a look at the interface. Here the process is simple :

  • Drag/Drop or select a CSV dataset (samples are provided alongside for download).
  • Start the processing using the Event.
  • Process 1: Parsing the CSV file (common for both javascript and Rust-Wasm)
  • Process 2 : QuickSort the array of data.
  • Process 3 : visualize the sorted data onto the page.

At this point, we will have multiple parameters calculated during the execution of the processes (parallel).

A good API makes it easy to improve the system by providing building blocks. Click to explore about, API Development Best Practices

What are the benchmarking outcomes during the test ?

Below are the benchmarks obtained during the testing :

As visible from these results, WebAssembly does not have any significant magic to offer with a lesser number of records, but as the count of the data increases, the Power of Wasm starts to outshine every other star ( javascript).

At merely reaching 1000 records, the WebAssembly overtakes javascript and just keeps on exponentially outperforming ever on. I would like to pin here that for 100k records, while Javascript took around 39 seconds to complete, Web-Assembly was done in merely 2 seconds. This goes to show the true potential of WASM.

Looking at these benchmarks, I was shocked but in a happy way. Happy to see what a great improvement WebAssembly has to offer to the Web world.

Use Cases of Wasm

WebAssembly has a vast variety of use cases with the ability to boost performances in all domains accessible.

Some possible uses of WebAssembly are :

  • Inside the browser

- Better execution speeds for existing toolkits and language on the web (C/C++, Rust, Go, etc.)

- Image/video editing

- High-end gaming

- CAD application

- Virtual Reality

- Local web Server

- VPN

- Fat clients for enterprise applications (e.g ., DataBases)

- Remote Desktop

  • Outside the browser
- Server-side applications and computers
  • Main-frame in web-assembly but UI in Js
  • Reusing existing code by targeting WebAssembly
Java vs Kotlin
Managed services for Enterprises to facilitate Automated Security Alerts, Single Click Deployments and Monitoring Solutions. Click to Talk to our Product Design and User Experience Solutions

Conclusion

The World Wide Web relies on JavaScript. Replacing it is a massive undertaking that will take decades. Furthermore, it is supported by a large community that is constantly working to enhance it. Yes, it is a reality that JavaScript has some drawbacks, but this cannot be the reason to abandon all technologies.

So, while WebAssembly will not be replacing JavaScript, it does not rule out the possibility of WASM's continued existence. Rather, it will become increasingly popular because WASM allows the web to perform complex computations like image processing or games. WASM can be used to create a fully functional web version of 3D games that can be played in a browser at 60 frames per second and higher and much more. It is a fact that javascript is the glue that wasm needs to be useful in the first place, and due to the whole Web practically running javascript, it will be a hard and long run for WebAssembly to be able to replace it completely.