最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Writing optimised JS for asm.js - Stack Overflow

programmeradmin2浏览0评论

There is quite a bit of excitement on asm.js and how it will be able to run some very heavy applications. However, it is piled from C++ code. Is it still possible to get the benefit of current improvements without knowing C++ or other low level languages?

Here is the thought that I had: Is it at all possible that we can write the code in Js, have it repiled for asm.js for optimisation?

There is quite a bit of excitement on asm.js and how it will be able to run some very heavy applications. However, it is piled from C++ code. Is it still possible to get the benefit of current improvements without knowing C++ or other low level languages?

Here is the thought that I had: Is it at all possible that we can write the code in Js, have it repiled for asm.js for optimisation?

Share Improve this question edited May 6, 2013 at 15:57 djechlin 60.9k39 gold badges171 silver badges300 bronze badges asked May 6, 2013 at 15:38 KayoteKayote 15.7k26 gold badges96 silver badges152 bronze badges 2
  • I trimmed this question as much as I could but I still don't know enough of asm.js to know what you're asking. – djechlin Commented May 6, 2013 at 15:58
  • It would have been nice if you had left the context in there, which was that as a non puter science front end developer, what hope do I have of being able to write super fast js without learning say C++. This edit has removed all context sadly. – Kayote Commented May 6, 2013 at 16:07
Add a ment  | 

4 Answers 4

Reset to default 6

If you have small function that is very putation-heavy (crunching numbers rather than manipulating DOM) you could rewrite it in asm.js style yourself, manually. It's possible (I've done it), but tedious.

There are other asm.js pilers, e.g. LLJS that you may use instead of C++.

However, asm.js is not magic. You will only get performance benefit when you use language that is much better suited for ahead-of-time optimization than JS. You can't take fully-featured JS and make it faster by running JS VM on top of JS VM, just like you can't make ZIP files smaller by zipping them.

However, it is piled from C++ code.

It is not. It's a language. Any program can emit text files which contain asm.js code. Emscripten piles LLVM IR into asm.js, and there are pilers from C and C++ to LLVM IR, but this is only one possible way of getting asm.js code. Admittedly, it's currently the most mature, practical and popular way, but I would not be surprised at all if other asm.js pilers for other languages pop up some time in the future.

Is it still possible to get the benefit of current improvements without knowing C++ or other low level languages?

Well, in theory any language that can efficiently be piled to machine code ahead-of-time can be implemented efficiently using asm.js, and that includes some rather high-level ones (e.g. Haskell). But currently, nobody has a working implementation, and I don't expect this to ever bee very popular. Right now, if you want asm.js performance, you'd probably write C or C++ code and pile it to asm.js, yes.

Note that the above excludes (among many others) Javascript.The fact that asm.js is a subset of Javascript is convenient in that asm.js code will run on unmodified browsers, but it's not of much use for anyone writing Javascript. asm.js is basically just a thin layer above machine code, with some amends for security and JS interoperability. Compiling JS to asm.js is as hard as piling it to machine code: Easy if you don't give a damn about performance (just always used boxed dynamically-typed values like an interpreter, and emit calls to runtime library functions), very hard when you do.

In fact, after decades of research into the subject, there's still no example of a highly dynamic language like Javascript, Ruby or Python being piled into machine code ahead of time and running much faster than a clever interpreter. Just-in-time pilation, on the other hand, is very much practical -- but the major JS engines already do that, in a less roundabout way than piling to asm.js, then parsing it again and piling it to machine code.

Asm.js isn't a separate language, but a subset of Javascript. It's just Javascript with a lot stripped out for performance. This means that you don't need to learn another language, though in this case knowing C/C++ might be useful to understand it.

Asm.js is a very strict subset of JavaScript, that can be generated relatively easily when piling from C/C++ to JavaScript. Asm.js code is much closer to machine code than ordinary JavaScript code, which allowed browsers to heavily optimise for any code written in asm.js. In browsers that implemented those optimizations, your code will typically run about 50% of the speed of a C/C++ program that is piled to machine code... which may seem slow, but is a hell of a lot faster than any ordinary JavaScript!

However, precisely because it's optimized for machines rather than humans, asm.js is practically impossible to handcode by any human developer... even though it's just JavaScript. While it is technically possible to convert - at least a subset of - ordinary JavaScript to an asm.js equivalent, such a conversion is not an easy task and I haven't encountered any project yet that made any attempt to achieve this.

Until someone achieves such a Herculean task, the best approach to producing asm.js code remains writing your code in C/C++ and converting it to JavaScript.

For more info on asm.js, see eg. John Resig's article from 2013 or the official specs.

发布评论

评论列表(0)

  1. 暂无评论