WebAssembly - JavaScript


Advertisements

This chapter will list out the comparison between WebAssembly and Javascript.

Javascript is a language, that we have used a lot inside the browser. Now, with WebAssembly release, we can also use WebAssembly inside the browser.

The reason for WebAssembly to come into existence is not to replace javascript, but to take care of certain things, that are difficult to handle with javascript.

For example

It is difficult to get the tasks such as Image recognition, CAD applications, Live video augmentation, VR and augmented reality, Music applications, Scientific visualization and simulation, Games, Image / video editing etc. to be done with javascript.

Using high level languages like C/C++, Rust, which now can be compiled to WebAssembly, it is easy to get the task mentioned above to be done. WebAssembly generates a binary code that is easy to execute inside the browser.

So here, is the list of comparison done between Javascript and WebAssembly.

Parameters Javascript WebAssembly

Coding

You can easily write code in Javascript. The code written is human readable and saved as .js. When used inside the browser you need to use a <script> tag.

The code can be written in text format in WebAssembly and it is saved as .wat. It is difficult to write the code in .wat format. It is best to compile the code from some other high level language instead of writing from start in .wat.

You cannot execute the .wat file inside the browser and has to convert to .wasm using the compilers or online tools available.

Execution

The code written in javascript when used inside the browser has to be downloaded, parsed, compiled and optimized.

We have WebAssembly code in .wasm already compiled and in binary format.

Memory Management

Javascript assigns memory when, variables are created and the memory is released when not used and are added to garbage collection.

Memory in WebAssembly is an arraybuffer that holds the data. You can allocate memory by using the Javascript API WebAssembly.memory().

WebAssembly memory is stored in an array format i.e. a flat memory model that is easy to understand and perform the execution.

The disadvantage of memory model in WebAssembly is −

  • Complex calculation takes time.

  • Webassembly does not support garbage collection that does not allow reuse of the memory and the memory is wasted.

Load Time & Performance

In case of javascript, when called inside the browser, the javascript file has to be downloaded, and parsed. Later, the parser converts the source code to bytecode that the javascript engine executes the code in the browser.

The Javascript engine is very powerful and hence, the load time and performance of javascript is very fast in comparison to WebAssembly.

A most important goal of WebAssembly is to be faster than JavaScript.Wasm code generated from high-level languages is smaller in size and hence, the load time is faster.

But, languages like GO, when compiled to wasm produce a big file size for a small piece of code.

WebAssembly is designed in such a way that it is faster in compilation, and can run across all the major browsers. WebAssembly still has to add lots of improvements in terms of performance in comparison to javascript.

Debugging

Javascript is human-readable and can be debugged easily. Adding breakpoints to your javascript code inside the browser allows you to easily debug the code.

WebAssembly provides the code in text format, that is readable but, still very difficult to debug. Firefox does allow you to view the wasm code in .wat format inside the browser.

You cannot add breakpoints in .wat and that is something that will be available in the future.

Browser Support

Javascript works well in all browsers.

All major web browsers have support for WebAssembly.

Advertisements