In a word: Yes.
V8 compiles the code into intermediate bytecode, then the "hot spots" are analyzed by the Crankshaft compiler, and if it determines that some variables will never be anything other than an integer, double, string, or so it generates machine code with unpacked.
It performs this optimization only on branches that have already been traversed; others are delayed until information about the actual types involved is calculated by the "regular" engine, and then entered into optimized code.
In addition, V8 can translate essentially static prototype hierarchies into "classical" inheritance of objects in C ++ to improve performance on "complex" types.
He can do all this only in code called โoftenโ; like loops or often called functions.
Associated with the article (part of the series) explains this in much, much more detail and is certainly worth reading.
EDIT: But of course, a statically typed language like Java will optimize as much code as possible at compile time, so it should outperform Javascript in all but toy tests. However, V8 closes the gap between them, and Javascript is much more fun to write than Java or C ++, so initial prototyping or developing programs where the user is the biggest source of latency means that Javascript is often the best choice. , in my opinion.
source share