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.

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.

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.

Dilemma for Microsoft

However it seems unlikely that Internet Explorer does the same: Microsoft has no interest in running Windows games on any other system ...
But if applications using Asm.js libraries are much faster on other browsers, it will be a cons-advertising for IE. So a dilemma ...
However do not expect to see all the commercial games ported on Asm.js because intermediate code, near the source is publicly provided and publishers generally prefer not to disclose their code. Open source software or software which use open source 3D engines will be the most ported.

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. Native Client

Which solution should you prefer, Asm.js, or NaCl, ie programs compiled to LLVM bitcode running also in their browser?

A program under NaCl works 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.
Knowing that Mozilla does not want to implement NaCl, that excludes it from Firefox, and apparently from Internet Explorer as well.

Asm.js works automatically, since a JS compiler already exists in each browser. We know how to optimize it and it takes only a few minor changes to work on V8 or other JIT (Just-In-Time) and AOT (Ahead-Of-Time).

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]); 
}

I can not guarantee that document.write is validated, it is there for demonstration.

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

Documents