What is WebAssembly (WASM)?#

WASM is a relatively new technology (ie. 2017) designed to facilitate high-performance applications on web pages.
Its promise of enabling near-native performance for web applications has piqued my interest, prompting me to explore its capabilities firsthand.

My goal#

My objective was to implement WASM in a live web environment to see for myself how to do it and how efficient it can be.
To achieve this, I needed to find a suitable function to benchmark WASM, implement it, cross-compile it, and integrate it into a web interface.

Benchmark#

Finding an appropriate benchmark for a programming language can be quite challenging due to various factors such as io-intensive operations.
Fortunately, I discovered “The Computer Language Benchmarks Game” which offers multiple algorithms specifically designed for benchmarking purposes. I choose Fannkuch-redux for my benchmark.

Fannkuch description#

For more background and implementation details, you can refer to the Benchmark Game site (see in sources).
Here’s a brief overview of the Fannkuch algorithm:

  • Take a permutation of {1,…,n}, for example: {4,2,1,5,3}.
  • Take the first element, here 4, and reverse the order of the first 4 elements: {5,1,2,4,3}.
  • Repeat this until the first element is a 1, so flipping won’t change anything more: {3,4,2,1,5}, {2,4,3,1,5}, {4,2,3,1,5}, {1,3,2,4,5}.
  • Count the number of flips, here 5.
  • Keep a checksum : checksum = checksum + (if permutation_index is even then flips_count else -flips_count)
  • Do this for all n! permutations, and record the maximum number of flips needed for any permutation.

My implementation#

I implemented the Fannkuch-redux algorithm in C, cross-compiled with emscripten with size-optimizations, and benchmarked it against a JS implementation. Both implementations are straight-forward, single-threaded and do not utilize any bitwise hacks or unsafe optimizations.

Test it yourself#

You can test the implementation yourself using the following code snippet:

Results#

Here are the result I gathered from my benchmarking:

F(12) JS WASM
Median 48950.98 ms 24050.48 ms
Mean 48536.23 ms 23985.03 ms

The results indicate a speedup of nearly 2 times when using WASM compared to JS!

Tempering the results#

It’s important to note that this is not a rigorous benchmark; multiple biases may exist in the results. However, I aimed to create a quick benchmark to illustrate the potential performance benefits of using WASM.

Conclusion#

My exploration of WebAssembly through the Fannkuch-redux benchmark has demonstrated its capability to significantly enhance performance for computationally intensive tasks. As WASM continues to evolve, it holds great promise for developers looking to build high-performance web applications. One of the most intriguing use cases for WebAssembly is its ability to package existing codebases written in languages like C, C++, or Rust for deployment on the web. This eliminates common issues related to dependencies, architecture, and system compatibility. With WASM, developers can simply open a webpage and deliver a near-native experience with their applications, regardless of the underlying platform.

Sources#