OdinMonkey and asm.js make it possible to write fast C/C++ apps for the web

Mar 22, 2013 11:31 GMT  ·  By

Mozilla is introducing OdinMonkey, its implementation of the asm.js spec. OdinMonkey makes it possible to compile purpose-built JavaScript code into quasi native code which runs much faster than JavaScript.

The end result is a massive performance improvement for apps that are designed to make use of asm.js. This, in turn, makes it possible to create games and powerful apps for the web that perform almost as fast as regular apps.

It gets better, asm.js was built with C/C++ to JavaScript compilers in mind, so it should make it possible to port C/C++ code to web-compatible JavaScript code and still be almost as fast as the native app based on that same code.

Here's how it all works. The asm.js specification is a subset of JavaScript designed to be easily compiled into quasi-native code. This compiled code would naturally be a lot faster than any JavaScript code can be.

However, in order to be easily compiled, asm.js requires a rather strict syntax and limits code to only some JavaScript features.

This makes it harder, though definitely not impossible, for humans to write code that asm.js can optimize. But asm.js wasn't designed with humans in mind, rather, it's designed for automatically generated code.

The main target is Emscripten, though it should work with other compilers, like Mandreel or LLJS, as long as they're instructed to compile for asm.js.

In practice, what this means is that developers can write C/C++ code, which is fast when running natively, but then compile/convert it into JavaScript code via Emscripten, for example.

Emscripten is already a great tool for this, making it possible to port many C/C++ applications to JavaScript.

This generated JavaScript code is going to be slower than compiling the C/C++ code as a native app, of course, but it will work in any modern browser out there.

Asm.js improves this, by ensuring that the generated JavaScript code is easily compiled into native code by JavaScript engines that implement the spec. The asm.js-compiled code is a lot faster than the regular JavaScript code, though still not as fast as the same code compiled for a native app.

The beauty of this is that engines that don't support asm.js optimization will still be able to run the JavaScript code, more slowly obviously.