Introduction to WebAssembly

By on April 13, 2018 12:18 pm

WebAssembly is an emerging standard for a low-level assembly-like language in a compact binary format that runs with near-native performance, and is available as a compilation target for a variety of languages. We’ve heard significant misunderstanding around WebAssembly and what it means for the web and JavaScript.

Is WebAssembly a replacement for JavaScript or TypeScript?

No.

Instead, WebAssembly is designed to run alongside JavaScript, and it is useful for a specific set of scenarios such as game engines, rendering systems, and other computationally expensive operations. WebAssembly serves similar use cases to other efforts such as WebWorkers, Canvas, WebGL, etc., but with a lower-level API.

Other languages (C#, Elm, Haskell, Java, PureScript, Python, etc.) have had the option to compile to JavaScript for years, independent of WebAssembly. WebAssembly makes other languages more efficient when compiled for usage in a WebAssembly environment, and also has the potential to enable features not possible with JavaScript.

WebAssembly makes other languages more efficient and also has the potential to enable features not possible with JavaScript.

Note that JavaScript may also compile to WebAssembly, which would be useful if there was an environment that supported WebAssembly, but not JavaScript. Engineers may also consider using asm.js when working with JavaScript code that needs optimization similar to that offered by WebAssembly.

If WebAssembly does not replace JavaScript, why is it needed?

JavaScript virtual machines cannot achieve native performance because of the overhead of JIT engines and the dynamic nature of JavaScript. In situations where you need more performance, WebAssembly gives developers tighter low-level control of code execution.

How does WebAssembly work with JavaScript?

There are a series of APIs for communicating between WebAssembly and JavaScript. Various WebAssembly compilers (such as emscripten for C or C++ code) will perform several operations, including compiling your source code into a WebAssembly binary, as well as generating JavaScript to load and initialize the WebAssembly binary code. Various compilers provide mechanisms for JavaScript code to access WebAssembly code, such as lifecycle hooks. Once the WebAssembly module loads, your JavaScript code can call into your non-JavaScript routines as plain JavaScript functions. Your non-JavaScript code can also invoke your JavaScript functions as well as a collection of WebAssembly APIs that are provided by the WebAssembly specification.

The WebAssembly MDN documentation contains numerous examples.

Predictions

In order for the web to remain competitive with native platforms, WebAssembly will be used for situations where JavaScript is not sufficiently fast, or for areas where significant engineering effort already completed in another language may not make sense to convert to JavaScript (for example, there is a rich history of game engines in other languages).

However, one of the primary benefits of the web is the near-universal nature of JavaScript, which has led to rapid improvements to the standard over the past five years. As such we hope and expect that most web source code will be authored in JavaScript (or relatively small supersets of JavaScript such as TypeScript), rather than authoring most web applications in alternative languages.

Current browser support

The latest versions of Chrome, Edge, Firefox, and Safari support WebAssembly including browsers for iOS and Android. WebAssembly is not available for IE11 or Samsung Internet browsers.


Next steps

Let's Talk! Logo

Do you need help creating your next high-performance app? Are you struggling to determine the right architecture for your next project? Contact us to discuss how we can help!

Support Logo

Get help from SitePen On-Demand Development, our fast and efficient solutions to JavaScript development problems of any size.

Contact Us Logo

Have a question? We’re here to help! Get in touch and let’s see how we can work together.