Asm.js, universal compatibility to applications

This subset of JavaScript defined by Mozilla gives apps and games nearly the speed of native code.

It was thought for a long time in the interest of a virtual machine in the browser to compile different languages ​​and make them work on all operating systems. This idea took shape thanks to Mozilla developers, and Asm.js is implemented in SpiderMonkey in 2013.

Capture from Unreal Engine 3 with Asm.js video

Video of Unreal Turnament in the browser

It is not a real bytecode but a subset of JavaScript that is compiled and optimized for speed. It is operational in Firefox. The asm.js code is compiled AOT (Ahead Of Time), produces a reusable code unlile JavaScript which is compiled JIT (Just In Time).

We can compile various statically typed languages ​​to LLVM bitcode, then use Emscriptem to convert it to Asm.js. The original code is already optimized, it produces faster JavaScript and Asm.js also brings speed to two times slower than binary but ten times faster than JavaScript.
An entire application can be converted into Asm.js or only a library from another language to be used in a JavaScript program .

With game developer Epic, Mozilla has implemented the 3D engine Unreal Engine 3 in Asm.js. This led to V8 developers to optimize the JavaScript compiler of Chrome and Node.js for Asm.js. Opera also has optimization for Asm.js code.
Microsoft, though, has announced the implementation of Asm.js in Windows 10 and for this, works with Mozilla.

Asm.js code since the last optimizations in December 2013 provides 1.5 times the execution time of native code to run the same program.

The new Edge browser from Microsoft, implements Asm.js, with the help of Mozilla.

Features

Diagram of the compilation into Asm.js
From C to LLVM bitcode to Asm.js

Asm.js is compatible with JavaScript, and provides no additional function but to be validated and compiled the code must only contain the subset defined by the specification.

One can benefit from the Asm.js language only by compiling a statically typed language because it is on this point that Asm.js get its speed. A dynamic language should rather be compiled to regular JavaScript to run in the browser.

Asm.js vs. WebAssembly

The first solution to build fast libraries for JavaScript was Native Client, by Google.

A program under NaCl (deprecated) worked in the browser but must be compiled for each operating system. So the author must provide multiple versions, that is not the case for Asm.js which works the same in any system.
Mozilla did not want to implement NaCl, Microsoft did not as well. It was removed in 2015 from Chrome.

Asm.js being a subset of JavaScript works automatically in each browser.

The announcement of WebAssembly in June 2015, made NaCl obsolete, it is the result of collaboration between all browser developers, whereas NaCl was only supported by Chrome.. But this is not the case of Asm.js with which it will coexist.
Wasm may be converted into Asm.js to run on older browsers. Wasm may also be used to make modules imported in JavaScript or Asm.js.

Example of code

The strlen function is defined and the letters of a string are displayed.

JavaScript:

function strlen(ptr) {
  var curr = ptr;
  while(MEM8[curr] != 0)
    curr++;
  return (curr - ptr);
}
 
var ptr = "demo";
var len = strlen(ptr);
for(var i = 0; i < len; i++) {
    document.write(ptr[i]); 
}

Asm.js:

function strlen(ptr) { 
   ptr = ptr | 0;
   var curr = 0;
   curr = ptr;
   while (MEM8[curr]|0 != 0) {
       curr = (curr + 1) | 0;
   }
   return (curr - ptr) | 0;
}

var ptr = "demo";
var len = strlen(ptr) | 0;
for(var i = 0; i < len; i++) {
    document.write(ptr[i]); 
}

Emscriptem with the option ASM_JS=1 produces Asm.js code rather than JavaScript.

Documents