Mozilla aims to compete with NaCl with open web technologies

Mar 22, 2013 19:11 GMT  ·  By
IonMonkey's modularity makes it possible to add technologies like OdinMonkey relatively easy
   IonMonkey's modularity makes it possible to add technologies like OdinMonkey relatively easy

Mozilla has just added support for asm.js in Firefox via its OdinMonkey extension to its new JavaScript engine, IonMonkey. This allows developers to create really fast JavaScript apps which run comparably as fast as native ones.

But there's a second, perhaps even more important benefit, asm.js was designed mostly for C/C++ ports to JavaScript.

In practice, it enables developers to convert their existing C/C++ code to JavaScript with relative ease and still be assured that it will run satisfyingly fast in the browser.

The approach is different, but this competes directly with Google's Native Client technology. Native Client is a Chrome technology which allows C/C++ apps to be delivered over the web and run on any platform with speeds close to running natively.

There are, though, big differences, both technically and philosophically. From a technical point of view, the big and obvious difference is that asm.js users JavaScript, Native Client doesn't.

With Native Client, developers compile their apps into Native Client binaries. These binaries are then distributed through the Chrome Web Store. Native Client binaries are very close to native code, but not quite.

They're just above raw native code in a way, and this one level of abstraction makes it possible for the same code to run on any machine for which a Native Client implementation exists.

It also means that Native Client apps are almost but not quite as fast as actual native apps. Still, as an added benefit they run in a Chrome sandbox.

Mozilla takes a different approach. C/C++ code is compiled into JavaScript code, specifically asm.js code, via Emscripten or other similar tools. This code is then distributed just like any JavaScript on any other site.

Clients that implement asm.js will be able to take this special JavaScript code and compile it into binary code. This happens as soon as the JavaScript code is downloaded, ahead-of-time (AoT) compilation as opposed to the JIT (just in time) compilation that most modern JavaScript engines do.

The resulting bytecode is significantly faster than JavaScript, but not quite as fast as a Native Client executable.

Converting bytecode to JavaScript, which is what Emscripten does, and then back to bytecode may not seem like a good idea. The big advantage is that JavaScript is understood by any browser out there, even if it doesn't implement asm.js, and can be distributed openly via standard means.

What this means is that Mozilla's technology is more open and more in tune with the open web. Google's version is faster though, at least for now. Mozilla itself touts the advantages of asm.js compared to Native Client (NaCl).

"The principal benefit of asm.js over whole new technologies like NaCl and PNaCl is that it works today: existing JavaScript engines already optimize this style of code quite well. This means that developers can ship asm.js today and it'll simply get faster over time. Another important benefit is that it's far simpler to implement, requiring very little additional machinery on top of existing JavaScript engines and no API compatibility layer," Mozilla explains.