Wait a minute: compiling code to an intermediate language so it can run on web browsers… this sounds a lot like Java! Do we really need another option for this?
There is a certain similarity in the concepts behind Java and asm.js. However, they’re solutions designed for different ages. Java applets are placed on a page and given a certain area that they are allowed to interact with. In other words, they were a single item on a larger page. This means that, while they have some uses, they have never really been suitable for full-on web apps.
So it already works everywhere? Does that mean I don’t need to change my browser?
It’s not essential, but like we said before, Firefox optimises itself when it detects asm.js code. This means that it will run much faster on recent versions of Firefox than it will on other browsers. Obviously, speed isn’t always essential, but when it is, you’re better off using Firefox for asm.js web apps. Chrome, after a slow start, is catching up. Other browsers are likely to perform worse at the moment, but may well see improvements in time.
So far, most of the work has focused on C and C++. The support for both of these is provided through the Emscripten source-to-source compiler. Since a large proportion of computer games are written in these languages, ams.js has been used to port games to the web (using WebGL for graphics). Perhaps the most famous asm.js project is the port of the Unity games engine (for example, Dead Trigger 2 – http://beta.unity3d.com/jonas/DT2 and AngryBots – http://beta.unity3d.com/jonas/AngryBots).
However, support for other languages is coming. Python has some support (via pypy.js), and the Lua VM can be built through Emscripten, but neither of these are really at the level of the C and C++ versions yet.
If something as computationally intense as an FPS game can run in asm.js, then most other software should have no problem.
It looks to me a little like most of the advantages of asm.js happen when you take something that is normally a native app and convert it into a web app. Isn’t this a bad idea? I mean, wouldn’t it be better just to compile the C or C++ to native code?
Whether or not it’s a good idea depends on many things, but basically it’s always a trade-off. Putting software in web apps can make them easier to access across a range of devices, but on the other hand, there are privacy and security concerns, and performance can be a problem. The idea of asm.js is to minimise the performance concerns as much as possible. In fact, benchmarks show that code compiled to asm.js can run at about twice the speed of the same code compiled natively. This might sound like quite a big slowdown, but it doesn’t mean that programs will run at half the speed, because only a small proportion of most software is actually waiting for a bit of code to run. Most of the time the computer’s waiting for user input, or for some data to be retrieved from the disk, or (in the case of games) a 3D scene to render on the graphics card. This means that plenty of software will appear to run at the same speed when using asm.js as when compiled to native code. This doesn’t change the trade-off between access on multiple devices and security, which will be highly dependent on the application and who’s hosting it.
This all sounds wonderful. How can I compile my C and C++ programs to asm.js?
Libraries are another matter. By default, asm.js includes libc, libc++ and SDL. If you want to work with other libraries, you could try compiling those libraries to asm.js, or re-implementing the features you need. There’s some more details on this on the Emscripten FAQ: http://kripken.github.io/emscripten-site/docs/getting_started/FAQ.html