React, etc. Tech Stack

React, Flux, GraphQL, Hack, HHVM...? All of this and more!

WebAssembly vs JavaScript - Streaming Compilation Performance Advantage

WebAssembly slowly started to seep into the minds of mainstream web developers. The lower level alternative to JavaScript now runs natively in major browsers. Streamin Compilation is a technique that can accelerate execution WebAssembly (aka Wasm) significantly.

The language is a more primitive language than JavaScript. This is a trade off that pays back in. WebAssembly (AKA Wasm) enables high performance computation to be done in the browsers. Some examples of such tasks are 3D games and video compression. It might be possible to use WebAssembly to emulate Flash in the future.

Wasm applications can be written by hand, but most commonly applications are written in higher level languages like C, C++, Python or Rust. The source library is then compiled to a WebAssembly binary format that can be executed by supported browsers - making Wasm an feasible alternative to JavaScript.

Streaming Compilation a big benefit for over-the-wire code

JavaScript in the browser is a high performance language. Engines like V8 from Google and Chakra from Microsoft optimize JS code on the fly with a technique called Just in Time Compilation (JIT). This is why raw execution speed between JavaScript and Wasm might not be significant in many cases.

WebAssembly has one fundamental advantage compared to JavaScript. JavaScript is parsed and the whole payload of the bytecode needs to be sent to the browser before it can parse and execute it. With large bundles this can be an issue. WebAssembly on the otherhand can start processing code as it comes, so even at same footprint (in Bytes) and runtime performance - the Wasm code will be able to boot up faster.

This advantage is unique to WebAssembly and is another interesting reason why you may want to look into compiling mundane UI code such as React, Svelte, etc. to WebAssembly.

For Node.js Streaming Compilation makes little difference

For Node.js the advantage is not there as it is run as a continuous process. Unless the Wasm provides a better runtime or capability performance otherwise, there is no inherent benefit because of streaming compilation - aside from a shorter startup time, which is insignificant in most cases.

Up until now, this was just a potential future speedup. But with the release of Firefox 58 it becomes a reality in shipping browsers. Wasm is very much here and provides a valid alternative to JavaScript in the browser - with some unique benefits for mobile devices sensitive to bytecode execution and byteload.

Written by Jorgé on Monday January 29, 2018

Permalink -

« Conditional Types arriving in TypeScript 2.8 - Microsoft disables Windows Spectre fix in an update »