Question

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 compile them to run on Web?

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

Was it helpful?

Solution

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 company, or even an important product, on it. Even if it succeeds, it will always be a niche technology compared 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 compilers emitting machine code-esque instruction.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top