質問

I've always viewed script languages (javascript, vscript, any kind of "script") as interpreted and therefore slower than compiled languages like C and C++.

"JavaScript is an interpreted language, with optional JIT-compilation support. In older implementations (e.g. Internet Exlorer 8 and earlier, Firefox prior to 3.5), JavaScript was a purely interpreted language. This means that scripts execute without preliminary compilation, i.e. without conversion of the script text into system-dependent machine code." -- http://www.javascripter.net/faq/whatisja.htm

In reading through the following, apparently google chrome (using V8) compiles javascript on the client:

"Here’s how it works. First, V8 defers compilation of JavaScript functions until immediately before they are executed the first time (to reduce the overall time spent compiling). Next, pieces of code that are executed very often are compiled a second time by a specialized optimizing compiler. This second pass makes takes more time, but thanks to many advanced optimization techniques, it delivers much faster code." -- http://thenextweb.com/google/2014/02/13/google-speeds-chrome-compiling-javascript-background/

It sounds like on V8, javascript is now a compiled language since they compile each function into machine code as soon as they see a function and before it is executed.

Additionally, this https://gist.github.com/spion/3049314 also appears to indicate (for this test case) that javascript on V8 is even faster than compiled C++ doing the same thing.

Does this mean that javascript is in fact compiled into machine code before it is executed on both the client and the server (since node.js uses V8) and therefore runs as complied machine code (like java, C, or C++ are) instead of interpreted code (like Perl, PHP, or Powershell are) with the related execution speed benefits?

Interpreted languages:

"The main disadvantage of interpreting is a much slower speed of program execution compared to direct machine code execution on the host CPU" -- http://en.wikipedia.org/wiki/Interpreted_language

Compiled languages:

"Compiled languages are always supposed to be fast because of their direct execution by the computer." -- http://www.codeproject.com/Articles/696764/Differences-between-compiled-and-Interpreted-Langu

役に立ちましたか?

解決

Yes, in 2014, everyone compiles JavaScript to native code using various strategies to optimize the code. There are even standards like asm.js to allow to compile JavaScript in such a way that the resulting code is at least as fast as code written in C/C++ or Java.

As always, the performance that you get in the end really depends on the optimizations and your code. Just as it's easy to write slow code in Java or C++, you can also write something that no automatic optimizer in the world can fix.

他のヒント

Yes it is compiled but compiling itself doesn't make anything fast, it's the optimizations the compiler does.

The fact that V8 doesn't have an interpreter at all unlike other engines is a design trade-off - it doesn't mean that interpreting is slower than compiled code that is unoptimized.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top