WebAssembly vs JavaScript: Can WASM Beat JavaScript In Benchmark?

Many people think WASM is lightning fast and will eliminate JavaScript eventually. However, there are a few points to consider.

It’s very likely that you’ve at least heard of WebAssembly and therefore you’re here. According to W3C, besides HTML, CSS, and JavaScript, WebAssembly is the fourth language for the web which allows code to run in the browser. This is a pretty huge event for web development because since 1996 (CSS), no language was added to this list. So, at this point, there are 3 significant questions raised.

  • What is WebAssembly and how does it work?
  • Is it really lighting fast as its introduction states?
  • Will it replace JavaScript?

Let’s start with the first question.

What Is WebAssembly?

WebAssembly is a low-level, compiled language that can be run on all major browsers. It is pretty new so its effects are not very obvious on the websites we use daily.

The primary concern of WebAssembly is speed. As you know, JavaScript is an interpreted language that is run on the browser using a JIT (Just In Time Compiler). The major drawback of this approach is that the code is not understandable by the computer until it is compiled at runtime. This slows down the execution process remarkably.

At this moment, WebAssembly comes strongly handy because, unlike JavaScript, WebAssembly is compiled all the way down to the binary codes, and therefore, it is so fast that its runtime speed is incomparable with JavaScript. But the story has not finished yet. There still are some points that should make you want to prefer JavaScript over WebAssembly because of speed concerns, surprisingly.

The other innovation that comes with WebAssembly is the availability of languages such as C, C++, Rust, or TypeScript on the web. Since WebAssembly is very low-level, it is kind of hard to code. Fortunately, there are compilers that compile the languages I mentioned down to WebAssembly. So, you can use your favorite language on the web to develop web applications.

Here is a sum function written in TypeScript. Since we will compile this function to WebAssembly, type definitions should be accordingly (AssemblyScript).

An AssemblyScript function to add two numbers.

#javascript #technology #webassembly

WebAssembly vs JavaScript: Can WASM Beat JavaScript In Benchmark?