Web programming languages: How WebAssembly will change the way you build web apps

What is WebAssembly, and how can developers get started with it?

The best programming languages to learn in 2019: Top coding skills that pay you the most These are the languages that are in the highest demand and offer the highest salaries.

What if you could write software once and have it run on every phone and computer?

While that dream may still be a long way from realization, moves are afoot to turn the web browser into a universal computing platform, capable of running even the most-demanding apps.

Helping to fulfill that vision is WebAssembly, a binary instruction format that allows browsers to run software anywhere from 10% to 800% faster than is typically possible using JavaScript.

The performance gains WebAssembly makes possible could one day see the heaviest of desktop software running in the web browser. WebAssembly is already being used to get the traditionally very taxing AutoCad software up and running as a proof-of-concept demo.

"My dream is that every platform will become the web platform," Ashley Williams, systems engineer at Cloudflare and member of the Rust core team, told the Qcon London 2019 conference.

"I genuinely believe the languages that will succeed in the future are the ones that are going to be able to target WebAssembly successfully."

WebAssembly is supported in the Chrome, Firefox, Safari and Microsoft Edge browsers today, as well as in the server-side JavaScript runtime Node.js. Several groups are also working towards a WebAssembly OS, with Williams highlighting work done under the WASMER project and by Mozilla under its Crane Lift initiative.

How to get started with Web Assembly

headshot3.jpg

Ashley Williams, systems engineer at Cloudflare.

Image: QCon London

WebAssembly (WASM) isn't designed to be a programming language that humans write, even if it can be viewed in a human-readable format. Rather it's a language that is generated by a compiler, based on code written by developers in a higher-level programming language.

Williams say that while in theory it's possible to compile code written in any language into WASM, in reality you wouldn't want to, with the best options currently being compiling from the C, C++ and Rust programming languages, rather than from an interpreted language.

"A thing to note about WebAssembly is because it doesn't have a GC [garbage collector] and doesn't have a runtime, you must compile your runtime in addition to your program when using an interpreted language," she said.

Here are various tools developers can use to start generating and inspecting WebAssembly:

Emscripten

This is a backend for the LLVM compiler that generates WebAssembly from C, C++ and Rust and that is used by the Unity and Unreal game engines to generate web-versions of games.

RustWASM

A compiler for Rust that generates WASM code that is capable of interoperating with JavaScript via JavaScript bindings. It works with JavaScript tools such as NPM, webpack, and Greenkeeper.

Williams says the plan is for the Rust toolchain to also generate JavaScript that can wrap the generated WebAssembly module, so it can be dropped into a browser or Node.js runtime environment as you would with JavaScript.

AssemblyScript

If you want to experiment with WebAssembly but don't fancy learning Rust, C or C++, there are compilers for higher-level languages.

Aimed at JavaScript developers, AssemblyScript is a language which is subset of the JavaScript spin-off TypeScript, and which compiles to WebAssembly via a toolchain called Binaryen.

"In general, the WebAssembly this produces is not necessarily the fastest or the smallest but it works very well and they are doing a ton of great work on it right now," says Williams.

WebAssembly Studio

WebAssembly.Studio is a fork of Visual Studio Code and runs in the browser, allowing developers to inspect WASM as compiled from C, C++ and Rust.

The state of WebAssembly

WebAssembly is designed to run alongside HTML, CSS and JavaScript in the browser, with the WASM module being loaded in using JavaScript APIs.

Ultimately, the goal of WebAssembly is not to replace JavaScript but to work alongside it, offering a level of performance that JavaScript can't match and allowing developers to write web apps in languages such as C++ and Rust, as well as opening the door for large amounts of desktop software to be ported to the web.

SEE: Hiring kit: Python developer (Tech Pro Research)

However, WebAssembly isn't a finished product, with plenty of room for improvement in both its support, features, and performance.

"WebAssembly is young, what has landed in the browser right now is certainly not a fully mature product," says Williams, giving the example of garbage collection not being implemented in WASM as yet.

"If you've started working with WebAssembly now, you'll immediately going to go 'Why is my WASM so big and why is it not as fast as I want it to be?'.

"It's because it's a new technology, but that being said it is designed to be faster than JavaScript and it is often faster, and, simply by function of it being an instruction set, your programs are going to be significantly smaller."

Williams is bullish on the prospects for WebAssembly, and with many people looking to the future of the web as it celebrates its 30th birthday, she has high hopes for how WebAssembly might transform the platform.

"I believe WebAssembly is going to lead to a future where the applications we're going to be making are very different from what we're seeing people make now."

Also see

By Nick Heath

Nick Heath is chief reporter for TechRepublic. He writes about the technology that IT decision makers need to know about, and the latest happenings in the European tech scene.