Javascript interpreted or compiled at runtime?

36

In this other question I asked the same thing, but in relation to Java. Now I ask about Javascript.

As far as I know, Javascript historically has always been interpreted, but Google has changed that with V8 on Chromium. Am I right or wrong? And in relation to other browsers , such as Firefox and IE, is it still interpreted?

    
asked by anonymous 22.07.2014 / 13:26

2 answers

24

Compiled at runtime. Like V8, other browsers introduced JIT Compilation to their JavaScript engines to improve performance, some almost parallel to the launch of V8 (and others later):

  • Internet Explorer: supports JIT from version 9, with engine Chakra ;
  • Firefox: supports JIT from version 3.5, with the TraceMonkey . According to Wikipedia, this was even the first implementation to use JIT, not V8, but their articles do not give exact dates. From then on, other JIT engines were also incorporated into SpiderMonkey;
  • Opera: uses V8 from version 15;
  • Safari: use the engine Nitro , also JITada in>, from 2008 (there is no mention of the version).

These are the main browsers . In the standalone context, we have Node.js which is based on V8, and the Rhino which is a special case: it originally operated in compiled mode from A to B, ie translating all JavaScript code to Java bytecodes (a practical example of the 3rd item in my answer to your other question ). This offered even better performance performance than the JIT build, but with the disadvantage of compile time being long. Today it supports the two modes: compiled and interpreted, so that the programmer can weigh what is most important to him in a given situation - speed vs. execution. speed of compilation.

In conclusion, all of the more common modern implementations use some kind of compilation, most of the JIT type. There may be others I do not know and, anyway, it's worth remembering what Maniero said in the same question     

22.07.2014 / 14:33
13

V8 and Rhino are examples of compilers of JavaScript. Respectively, for internal native code (C ++) and for JAVA Bytecode. There are also other interpreters that use a JIT compiler.

By default, JavaScript is both interpreted and compiled - this specification is not necessarily the responsibility of the language, but in the JavaScript scenario, you can delegate which interpretation or build engine you want to use.

If you want to use V8, Google explains how to work on your engine, which, as already mentioned, is compiled. Naturally web-based applications, in turn, will use engines and devices provided by client browsers, which carry interpreter mechanisms and will not compile JavaScript in a standard pattern.

Beware here: precompilation of assets

A recurring error is confusing compilation of assets with language compilation. When we use Rails, for example, we can use the Asset Pipeline to precompile our assets, causing X of .js files to unify and compress to form a single, mini- in a (erroneous) sense of "compilation."

Actually, there was yes a compilation, but it not is the same as what you are thinking, like what C # or JAVA does.     

22.07.2014 / 14:21