asm.js defines a subset of JavaScript that can be compiled to fast executables. It has been created at Mozilla by David Herman, Luke Wagner and Alon Zakai. According to the specification, “[asm.js] effectively describes a safe virtual machine for memory-unsafe languages like C or C++.”
This blog post describes how asm.js works, it is based on the specification.

Things that slow down JavaScript

Current JavaScript is already quite fast, but a few mechanisms in engines limit its speed:

Boxing: Floating point numbers (including integers stored as floating point numbers) are boxed, they have wrappers that allow them to co-exist with other values such as objects.

Just-in-time (JIT) compilation and runtime type checks: Most JavaScript engines compile code in two stages. Initially, a format is used that can be compiled to quickly, but that runs slowly (e.g. interpreted bytecode). The execution of that format is observed. If it runs more often, assumptions can be made about the types of its parameters etc. and it can be compiled to a format that runs faster. If one of the assumptions turns out to be wrong, the faster format can’t be used any more and the engine has to go back to the slower format. The faster format is always slowed down by checking whether the assumptions still hold.

Automated garbage collection: which can be slow.

Flexible memory layout: JavaScript’s data structures are very flexible, but they also make memory management slower.

asm.js code can produce executables that exhibit none of these drawbacks. They can be compiled “ahead of time” (before execution) and are faster than JIT-compiled ones:

“asm.js can be implemented massively faster than anything existing JavaScript engines can do, and it’s closing the gap to native more than ever.”
– David Herman

The asm.js specification only describes what JavaScript code has to look like to be asm.js-compliant, the semantics follow from the ECMAScript language specification. That is, asm.js is a true subset of JavaScript.

How asm.js works

asm.js code is packaged in specially marked functions (“asm.js modules”) that have the following structure:

The annotations tell the compiler what type to expect and also coerce arguments to the correct type.

Supported types

Value types.
asm.js supports:

64 bit double-precision floating point numbers. Type annotation: +x

32 bit integers (ignoring several types that are needed so that all asm.js-supported JavaScript operations can be typed correctly). Type annotation: x|0

32 bit floats. Type annotation: Math.fround(x)

asm.js’s doubles are the same as JavaScript’s. You can’t work with integers directly in JavaScript, but 32 bit integers are used internally [1].

References types.
Reference types are only allowed for variable declarations at the top level of a module. All other variables and parameters must have value types. The following reference types are available:

Checking for asm.js conformance

Static checks (at compile time).
The code of an asm.js module is statically checked (when the JavaScript code is loaded): It must only use the declarations, statements and expressions that are part of asm.js (e.g. almost none of the OOP features of JavaScript are). And it must be well-typed, according to the static type system. If the checks fail, then asm.js code can’t be AOT-compiled and is executed as normal JavaScript code.

Dynamic checks (at link time).
When you invoke an asm.js module, the following dynamic checks are performed. If one of them fails, the AOT-compiled code can’t be linked and the engine must fall back to normal JavaScript.

No exception must be thrown until the return statement is reached.

The heap object (if provided) must be an instance of ArrayBuffer. Its byteLength must be a multiple of 8.

All view objects must be true instances of their respective typed array types.

All properties of the stdlib object must implement the semantics as specified by the ECMAScript standard. In practice, that means that they must have the same values as the properties of the global object that have the same names.

Advantages of asm.js

The approach taken by asm.js has the following benefits:

Relatively easy to implement on top of existing JavaScript engines. Quoting David Herman:

[...] it’s significantly easier to implement in an existing JavaScript engine than from-scratch technologies like NaCl/PNaCl. Luke Wagner has implemented our optimizing asm.js engine entirely by himself in the matter of a few months.

Interacts well with JavaScript. It is a subset of JavaScript, after all.

Backward compatible with all existing JavaScript engines: if an engine isn’t aware of asm.js then the code simply runs as normal JavaScript.

Emscripten and the performance

To see what is possible with asm.js, you only have to look at Emscripten which is described as follows on its web site:

Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode (which can be generated from C/C++, using llvm-gcc or clang, or any other language that can be converted into LLVM) and compiles that into JavaScript, which can be run on the web – or anywhere else JavaScript can run.

The list of projects that have been compiled via Emscripten is impressive: SQLite, Graphviz, LaTeX, Python, etc.

Emscripten already produces surprisingly fast code. In fact, its way of code generation has been the inspiration for asm.js and its creator Alon Zakai is part of the asm.js team. A modified version of Emscripten now targets asm.js, which results in considerable performance increases on engines with the necessary support.

Initially, C code compiled to asm.js ran at 50% of native speed.
Support for 32 bit floats pushed performance to approximately 70% of native.

The future

Several features are on the horizon for JavaScript that will also benefit asm.js:

Modules: ECMAScript 6 will have modules, then asm.js code can be packaged more conveniently.

More value objects are also planned for ECMAScript, with 64 bit integers having priority. Once JavaScript has them, they are also available to asm.js.

Furthermore, asm.js might be expanded to be a more full-fledged virtual machine in the future, similar to the Java Virtual Machine or Microsoft’s Common Language Runtime. Quoting the asm.js FAQ:

Right now, asm.js has no direct access to garbage-collected data; an asm.js program can only interact indirectly with external data via numeric handles. In future versions we intend to introduce garbage collection and structured data based on the [ES7] structured binary data API, which will make asm.js an even better target for managed languages.

Support in JavaScript engines

Firefox: optimized for asm.js since version 22

Chrome and Opera: are optimizing for asm.js without requiring the directive 'use asm';

Conclusion: the best of both worlds

asm.js is impressive technology. There will probably always be people who would prefer a single standardized bytecode for the web, but asm.js proves that different approaches are possible and beneficial. It gives you the best of both worlds:

Low-level code: use asm.js for computationally intensive tasks and as a target language for compilers. The latter is the dominant asm.js use case. That is, it is not meant to be written by hand, but to be generated via tools such as Emscripten (source languages: C, C++) and LLJS (source language: static dialect of JavaScript).

High-level code: use all of JavaScript for maximum flexibility.

The new role of JavaScript source code.
JavaScript source code becomes a format for delivering programs that abstracts over the different compilation strategies of JavaScript engines [5] and over the difference between asm.js and JavaScript. Delivering source code is not an approach that opposes a particular compilation strategy (bytecode etc.), you simply postpone deciding on one, giving engines the freedom to make their own choice. Additionally, this approach allows you to compile asm.js code in the browser: simply assemble a text string with the code and use eval() or (better) Function() to compile it.

JavaScript engines are optimized for higher-level code in a manner that can’t be replicated by less specialized engines. Thus, there will always be a schism between low-level and high-level engines; asm.js manages to make that schism as small as possible.