JavaScript Assembly Language
Written by Ian Elliot   
Friday, 22 February 2013

The idea of using JavaScript as a modern day assembly language for browser code is being taken very seriously by Mozilla. Asm.js is a specification for a high performance JavaScript assembly language and OdinMonkey is an engine that runs it really fast.

A compiler doesn't have to target a low level machine code. For example, the Java compiler creates byte code which is then run using a virtual machine. JavaScript is so ubiquitous that it makes sense to use it as a target for compilers of other languages so that web apps can be written using them. The best known example is probably CoffeeScript which compiles to standard JavaScript. However, CoffeeScript is already quite close to JavaScript and the compilation is a fairly easy task.

More difficult is to compile C++ to JavaScript, which is what compilers like Emscripten or Mandreel do. At the moment these compilers attempt to render C++ into JavaScript so that the result runs reasonably fast. A much better idea is to define a subset of JavaScript, which is complete in the sense that you can do anything you need to using it, but one that can be implemented efficiently.

 

odindiagram

 

This is what asm.js is all about. It is a detailed specification for a subset of JavaScript. Notice that while this means that this isn't a new language there are new features which would be ignored by a standard JavaScript engine. For example, the language only has strictly-typed integers, floats, arithmetic, function calls, and heap accesses. This is achieved using annotations in the form of comments and a restricted set of operations which only work with the data type - for example logical operators convert numeric values to int32.

The fact that asm.js is a subset of JavaScript means that you can take an asm.js program and run it in a standard unmodified browser or JavaScript engine. However, to get any real value from using it you need it to be run using an optimizing JavaScript engine. This is the second half of the project. Mozilla is working on OdinMonkey, a JavaScript engine that recognizes when it has been fed an asm.js program and can apply automatic optimizations.

The target for for OdinMonkey is to run compiled C++ code from Emscripten at no worse than half the speed. Current benchmarks seems to prove that it is possible. You can also try it out by compiling using emscripten with the ASM_JS=1 switch and then run it using a special Firefox build.

 

odinbenchsmall

Click for larger version

 

So, putting the ideas together: build compilers from C++ and other languages to asm.js and use optimized JavaScript engines to run the code at near native speeds. If the browser doesn't have an optimized engine then the code still runs because its just JavaScript.

It remains to be seen if other JavaScript engines take up asm.js. The alternative is of course Google's NaCL and PNaCL, but again only one browser manufacturer is supporting it. We will have to wait while they fight it out.

odindiagram

Banner


MIcrosoft's Project Oxford AI APIs For The REST Of Us
01/05/2015

An announcement that has mostly gone unnoticed at this year's Build is Project Oxford - a set of REST APIs that give you some advanced AI in a really easy-to-use form. 



Push Notifications Come To Chrome And Android
22/04/2015

The biggest problem the web has is its lack of push. Something new might be published, but you have to remember to navigate back to the page to see what it is - you have to contact the web page. Chrom [ ... ]


More News

Last Updated ( Wednesday, 06 March 2013 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2015 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.