3

I know that V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, or MIPS CPUs) before executing it. And I have read that google native client compiles C/C++ to machine code, so if those two technologies return same result(machine code), What's the difference between them except for the used languages(JavaScript C/C++)?

Taron Mehrabyan
  • 2,199
  • 2
  • 20
  • 27
  • The result is not the same at all. The unoptimizing JS compiler will just emit a lot of runtime calls to complicated functions in the code, and even compared to the output of optimizing compiler, the code generated from C/C++ will be much tighter. – Esailija Oct 10 '13 at 18:29

2 Answers2

2

Well, given a CPU architecture (say, you run on an Intel box) you can say that any technology ends up compiling to machine code, right? So Python, Perl, Javascript, C++, Fortran and so on are just different languages that get compiled down to machine code (Python and Perl are usually bytecode VMs but these also run as machine code down in the bottom).

v8 is a Javascript runtime. Yes, it uses compilation under the hood to speed up your code. Other JS runtimes do that too (*monkey of Firefox, etc.)

NaCl (through PNaCl or not) lets you write C/C++ code that ends up executing in the browser. This has some advantages and disadvantages vs. JS, and which ever you pick depends on your specific needs. For most applications JS is more suitable because it's a higher-level language so it's more convenient to program in. Some applications, however, need special levels of performance that's not achievable with JS (at least at this time). These applications benefit from having a NaCl module inside, that usually takes part in a larger architecture which includes JS as well.

Read this for more details.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • I agree with you. Was't it possible to give some API's for the above mentioned cases? Were they worth to bring a new language into web? – Taron Mehrabyan Oct 08 '13 at 21:55
  • @TaronPro: this is an involved question, and an answer would have to consider many angles. Think about existing C/C++ applications, for example. Their creators don't necessarily want to rewrite the whole application in a different language and/or give up performance. For them, the more interesting question is "can I use my application on the web with minimal changes, while keeping it fast?" – Eli Bendersky Oct 08 '13 at 22:10
  • Yes, but in any case these programs will not work fully as in desktop, they will be in sandbox and will restrict the possibilities of C++ – Taron Mehrabyan Oct 09 '13 at 07:35
  • @TaronPro: the restrictions are minimal, and more will be lifted with time (as long as the security of the sandbox is maintained). – Eli Bendersky Oct 09 '13 at 13:04
0

The main difference is, that JavaScript is supported by every browser and NativeClient is Chrome (Chromium) only. So, if you want to write a Web-Apps JavaScript is the way to go, because it will run in (almost) every browser, not just in Chrome.

Christoph
  • 315
  • 1
  • 5