The Idea of Asm.Js Is to Minimise Performance
Total Page:16
File Type:pdf, Size:1020Kb
FAQ ASM.JS ASM.JS Bringing near-native performance to cross-platform web apps. BEN EVERARD Any JavaScript code that only uses this browsers support JavaScript out of the subset can be said to use asm.js. box without the need for plugins. This However, since it’s valid JavaScript, it means that you don’t need to ask users ASM usually means assembly, will run on any JavaScript engine. As to download anything in order to and .js usually means a there are JavaScript engines for almost access your site. JavaScript library, but what on earth every computing platform built in the are those two things doing together? past decade, compiling code to asm.js Why bother using a subset The idea behind asm.js is to means it should be very cross-plaform. of JavaScript, when the full remove everything from language is already supported on JavaScript that doesn’t run quickly. The Wait a minute: compiling code multiple browsers on most modern result is a very strict subset of to an intermediate language so operating systems? JavaScript that isn’t as nice to program it can run on web browsers… this The advantage of limiting the in, but does run much faster. sounds a lot like Java! Do we really available options is speed. Using Although asm.js is an interpreted need another option for this? Firefox (which is the browser that language that you could program in, There is a certain similarity in handles asm.js best), the JavaScript programmers will usually write code in the concepts behind Java and engine is able to detect when a another language, and then compile asm.js. However, they’re solutions particular script is written in asm.js, and that language to asm.js. In other words, designed for different ages. Java will optimise itself accordingly. This asm.js is designed to be a little like applets are placed on a page and given gives it the advantage of running assembly language, but it’s actually a certain area that they are allowed to everywhere (because it is a subset of JavaScript. interact with. In other words, they were JavaScript), yet being able to run very a single item on a larger page. This quickly when the JavaScript engine So it’s just another means that, while they have some uses, supports it. JavaScript engine? they have never really been suitable for No. In essence, asm.js isn’t full-on web apps. So it already works anything other than a JavaScript (and by extension asm.js) everywhere? Does that mean I specification of a subset of JavaScript. can interact with the entire web page. It don’t need to change my browser? can add, remove and manipulate items It’s not essential, but like we said in the HTML in an almost endless before, Firefox optimises itself The idea of asm.js is to series of ways. In other words, when it detects asm.js code. This “ JavaScript can be used to control the means that it will run much faster on minimise performance entire web page. This makes it a much recent versions of Firefox than it will on concerns as much as possible. better option for modern web apps. other browsers. Obviously, speed isn’t ” What’s more, almost all modern web always essential, but when it is, you’re 40 www.linuxvoice.com ASM.JS FAQ 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. You mentioned earlier that programmers write in other languages, and then compile to JavaScript. What languages can you program asm.js in? 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 If something as computationally intense as an FPS game can run in asm.js, then most written in these languages, ams.js has other software should have no problem. been used to port games to the web (using WebGL for graphics). Perhaps devices, but on the other hand, there are This all sounds wonderful. the most famous asm.js project is the privacy and security concerns, and How can I compile my C and port of the Unity games engine (for performance can be a problem. The C++ programs to asm.js? example, Dead Trigger 2 – http://beta. idea of asm.js is to minimise the Software can be compiled to asm.js unity3d.com/jonas/DT2 and AngryBots performance concerns as much as using Emscripten. Use this in exactly the – http://beta.unity3d.com/jonas/ possible. In fact, benchmarks show that same way you would any other AngryBots). code compiled to asm.js can run at compiler. Asm.js is used when you set However, support for other languages about twice the speed of the same the optimisation flag to -01 or higher. is coming. Python has some support code compiled natively. This might This can output pure JavaScript or an (via pypy.js), and the Lua VM can be sound like quite a big slowdown, but it HTML file that includes the JavaScript. built through Emscripten, but neither of doesn’t mean that programs will run at See the tutorial at http://kripken. these are really at the level of the C and half the speed, because only a small github.io/emscripten-site/docs/ C++ versions yet. proportion of most software is actually getting_started/Tutorial.html for a waiting for a bit of code to run. Most of useful look at how to get started. Why not just skip this step and the time the computer’s waiting for user write in JavaScript? input, or for some data to be retrieved What about stuff that There are a few reasons! There’s from the disk, or (in the case of games) JavaScript in the browser just obviously a lot of legacy code a 3D scene to render on the graphics can’t do, like access the filesystem, that exists already in C and C++, so why card. This means that plenty of and link to libraries. bother re-writing it in JavaScript if you software will appear to run at the same There’s no way that asm.js can can just compile it? You might want a speed when using asm.js as when access the filesystem of a single codebase that can compile to compiled to native code. This doesn’t machine when running on a website – both native and browser. Also, compiled change the trade-off between access JavaScript is deliberately kept separate asm.js code tends to be quite a bit on multiple devices and security, which from the machine it’s running on for faster than hand-writen JavaScript will be highly dependent on the security reasons. However, asm.js because it takes advantage of a whole application and who’s hosting it. programs can access a virtual file host of optimisations. We should also point out here that system. This enables the developer to although JavaScript is usually used for use the same C and C++ code that It looks to me a little like most web apps, you don’t have to use it this accesses files, but at the same time, of the advantages of asm.js way. There’s nothing to stop you using still protect the host machine from any happen when you take something asm.js to create software that doesn’t malicious asm.js code. that is normally a native app and rely on the network, and just uses the Libraries are another matter. By convert it into a web app. Isn’t this a JavaScript engine to provide portability. default, asm.js includes libc, libc++ and bad idea? I mean, wouldn’t it be If asm.js takes off, we’re likely to see SDL. If you want to work with other better just to compile the C or C++ more and more software doing this. In libraries, you could try compiling those to native code? fact, it’s already possible to compile libraries to asm.js, or re-implementing Whether or not it’s a good idea some Qt software to asm.js. There are the features you need. There’s some depends on many things, but some examples at http://vps2. more details on this on the Emscripten basically it’s always a trade-off. Putting etotheipiplusone.com:30176/ FAQ: http://kripken.github.io/ software in web apps can make them redmine/projects/emscripten-qt/wiki/ emscripten-site/docs/getting_started/ easier to access across a range of Demos. FAQ.html www.linuxvoice.com 41.