admin管理员组

文章数量:1289583

Emscripten could generate faster code from C/C++ than JS code wrote by hands, so is that means we should write new code in C/C++ and pile them to run on Web?

I read the Emscripten FAQ, it says "By all means write new JavaScript code.", why is that?

Emscripten could generate faster code from C/C++ than JS code wrote by hands, so is that means we should write new code in C/C++ and pile them to run on Web?

I read the Emscripten FAQ, it says "By all means write new JavaScript code.", why is that?

Share Improve this question asked May 13, 2013 at 16:50 allfoxallfox 1131 silver badge4 bronze badges 3
  • 2 Don't touch C++ unless you can actually feel the difference between your JS and asm.js . – user1233508 Commented May 13, 2013 at 16:51
  • Very rarely does JavaScript actually "do" much enough to make any difference. Sure, there are some javascript that takes some time to do it's job, and there is some badly written javascript that takes far too long to run. But piling it doesn't really solve the latter. – Mats Petersson Commented May 13, 2013 at 16:54
  • @allfox Your mixing 2 things here. 1. Cross piling from C/C++ to Javascript might be faster and 2. Using asm.js over Javascript will almost always be faster. But do you want to know about the former or the latter? – abergmeier Commented May 14, 2013 at 15:31
Add a ment  | 

1 Answer 1

Reset to default 12

asm.js is not a faster way to execute Javascript-esque code. It is a faster way to run code which is already reduced to the level of abstraction of machine code. You seem to greatly overestimate the gains:

  • If you let JS developers write C++ as if it's JS, you end up with fugly code that's not nearly as fast as C++ could be and flawed in other ways too.
  • Many potential bottlenecks, such DOM manipulation and network latency, aren't affected at all by how fast your code is running.
  • For many programs, the speed up from a faster language implementation is dwarfed by the speed up of high-level optimizations. In other words, doing work somewhat faster is nice, but not doing it at all is even faster.

Going this route has significant downsides as well:

  • You have to throw away your working code, and re-write it (including bugs) in a language most of your team won't know nearly as well, if at all.
  • As of now, the technology is still in its infancy. You wouldn't bet your pany, or even an important product, on it. Even if it succeeds, it will always be a niche technology pared to JavaScript. This doesn't disqualify it for professional work, but it will make many things harder.
  • IIUC, you can't directly do most things JS can do beyond crunching numbers, you can only call into JS functions explicitly offered to the asm.js module. That is, you'll always need at least a bunch of glue code in Javascript, and (as mentioned above) if this includes your bottlenecks, you didn't actually gain anything.

The only kinds of code which I expect to gain enough from asm.js to use are:

  • Existing code which isn't already written in JavaScript. For the sole reason of saving you most of the porting hassle.
  • Heavy number crunching not interacting with the browser. (Think: How often do you do this? And do you really want to go through the effort of writing it in C/C++ and wiring it up with JS?)
  • Stuff which is, by its nature, at the level of abstraction asm.js supports, such as pilers emitting machine code-esque instruction.

本文标签: