Is Javascript interpreted or compiled at runtime?

Asked

Viewed 8,746 times

38

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

As far as I know, historically Javascript has always been interpreted, but Google has changed that with V8 on Chromium. Am I right or wrong? And as for the others browsers, as Firefox and IE, is still interpreted?

  • That’s a good question

2 answers

29


Compiled at runtime. Like V8, the rest browsers introduced JIT Compilation to their Javascript engines to improve performance, some almost parallel to the V8 release (and others later):

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

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

In conclusion, all the most common modern implementations use some kind of compilation, most of the JIT type. There may be others I don’t know and, anyway, it’s worth remembering what Maniero said in the same question linked: "We cannot say that there are languages interpreted or compiled or even Jittadas. At most we can say that implementations have these characteristics. And they are not mutually exclusive."

  • This explains why Opera is always picking up my animations while other browsers run perfectly

14

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

To the letter, 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 compilation engine you want to use.

If you want to use the V8, the Google explains how to work on your engine which, as already said, is compiled. Naturally web applications, in turn, will use the engines and devices provided by the client’s browsers, which bring with them interpreting mechanisms and which will not compile Javascript in a standard.

Watch out here: precompilation

A recurring error is confusing compilation of Assets with language compilation. When we use Rails, for example, we can use Asset Pipeline to precomp our Osets, causing an amount X archival .js unite and compress to form one, minified, resulting in a (erroneous) feeling of "compilation".

In fact, there was yes a compilation, but she nay is the same one you’re thinking of, like what C# or JAVA do.

  • 2

    For those working with ASP.NET MVC, this precompilation of Assets is also known as Bundle and minification - http://msdn.microsoft.com/pt-br/library/dn168847.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.