WebAssemby Another Milestone
Written by Ian Elliot   
Tuesday, 01 November 2016

WebAssembly is most likely the next step for programs running in the browser and it is inching forward towards a release. The latest step is a coordinated preview in Chrome, Firefox and Edge, only leaving the question of where is Apple's Safari? What does WebAssembly mean for the future?

First we need to be clear what the big idea is. 

We all know that JavaScript is the language of the web and, while it is a sophisticated language, its sophistication makes it difficult to optimize. While great improvements have been made in speeding up JavaScript it is generally thought that it is now running about as fast as it can go with only minor improvements to be had. 

The need to achieve native code speeds is something that has to be addressed. As web browsers become the host for what used to be desktop application we need the extra speed. We also need a way to write in languages other than JavaScript. 

The first attempt at a solution is asm.js which isn't a new language but a subset of JavaScript. You can think of it as JavaScript written in a particular way that makes it possible for the engine to assume things that allow it to make the code run faster. Using asm.js does speed things up but perhaps not enough. It can also be used as a target for compilers like Emscripten to allow C/C++ code to run in the browser. 

WebAssembly, or wasm, goes a step further than asm.js. It isn't a subset of JavaScript but a binary representation of a program in a byte code for a stack machine. In other words, WebAssembly is run on a virtual machine in the same way that Java is run on the Java Virtual Machine. WebAssembly is a low level language for a simulated machine. The V8 team's blog post comes with an example of what wasm looks like:

Raw Bytes
Text Format
C Source
02 40
03 40
20 00
45
0d 01
block
 loop
   get_local 0
   i32.eqz
   br_if 1
while (x != 0) {
 
20 00
21 02
   get_local 0
   set_local 2
 
 z = x;
20 01
20 00
6f
21 00
   get_local 1
   get_local 0
   i32.rem_s
   set_local 0
 
 
 
 x = y % x;
20 02
21 01
   get_local 2
   set_local 1
 
 y = z;
0c 00
0b
0b
   br 0
 end  
end
 
 
}
20 01
get_local 1
return y;

 

The raw bytes in the first column are what are sent to the browser to run. The second column shows the assembly language version and the third is the corresponding C source that is complied to wasm.

The compiler outputs the raw bytes and this is not instantly human readable, but the browser can convert it to the text format automatically. Whether you regard this as human readable or not depends on how much assembler you have encountered. It is clear that we are moving away from the era where code in web pages was fairly easy to read. 

What has all this got to do with JavaScript? 

The answer isn't what you might expect. WebAssembly is loaded, run, and generally controlled by  JavaScript. There is a WebAssembly object and you have to use this to get the WebAssembly code, compile the bytes and run it. In the future the idea is that wasm modules will be loaded into the browser just like JavaScript. 

The Firefox blog gives a recent graph comparing wasm, asm.js and native C/C++:

wasm

 

All of this is exciting but before you can use it the browsers have to implement it. The Chrome blog says:

Today we’re happy to announce, in tandem with Firefox and Edge, a WebAssembly Browser Preview. WebAssembly or wasm is a new runtime and compilation target for the web, designed by collaborators from Google, Mozilla, Microsoft, Apple, and the W3C WebAssembly Community Group.

The milestone involves: 

  • a release candidate for the MVP (minimum viable product) design (including semantics, binary format, and JS API)

  • compatible and stable implementations of WebAssembly behind a flag on trunk in V8 and SpiderMonkey, in development builds of Chakra, and in progress in JavaScriptCore

  • a working toolchain for developers to compile WebAssembly modules from C/C++ source files

  • a roadmap to ship WebAssembly on-by-default barring changes based on community feedback 

This just about makes the whole thing usable as an experiment, but because the feature isn't in the production browsers you can't actually expect to ship the code to end users. 

Notice the mention of Apple in the announcement. The Webkit browser is supposed to be developing wasm support, but they are doing quietly on their own and without joining in the releases. It isn't currently clear if Webkit and Safari will eventually ship with wasm. Why is Apple hanging back? Probably because the company hasn't made up its mind. Apple controls what runs under iOS and apps are a key attraction of the platform to the end user. If wasm becomes a reality there could well be little difference in performance between a native app and a wasm app and this might mean that Apple would lose control of the iOS app space. This is obviously speculation about Apple's motives but it is undeniable that it is the odd one out of the big browser makers when it comes to work on wasm. 

Is this a bright new dawn that will see the death of JavaScript?

The first thing to say is that many programmers like and admire JavaScript's design - it is one of the few languages that doesn't slavishly follow the lead of Java and C++. Even if you do want to drop JavaScript, there is little prospect of this being a reality for some time. At the moment JavaScript provides much of the infrastructure that wasm uses and the two are likely to coexist for a long time. 


webassemlogo

 

More Information

Chrome

Firefox 

Edge

http://webassembly.org/

Related Articles

WebAssembly Takes A Big Step Towards Being Real 

Progress On WebAssembly 

WebAssembly Has Mozilla, Microsoft,Apple and Google Backing It 

Asm.js Goes All Edgy 

Support For Asm.js Growing?       

Mozilla Enhances Browser-Based Gaming       

WebKit JavaScript Goes Faster Thanks To LLVM       

Firefox Runs JavaScript Games At Native Speed  

Asm.js Gets Faster       

Java, ASM.js Or Native - Which Is Faster?       

JavaScript Assembly Language  

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin

Banner


Amazon Adds AWS Lambda Code Editing Tool
04/11/2024

Amazon has added a new code editing option for AWS Lambda in the AWS console based on the Code-OSS, Visual Studio Code Open Source code editor.



Firefox 1.0 Released 20 Years Ago
10/11/2024

A news item with the headline "Firefox browser takes on Microsoft" from 20 years ago has attracted renewed attention. It was originally published on the BBC News website on November 9th, 2004 rec [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

 

 

Last Updated ( Tuesday, 01 November 2016 )