37

I'm trying to figure out if web browsers use an interpreter to execute javascript, or some sort of compiler. It is well known that scripting languages are interpreted not compiled; however there is the JScriptCompiler that can compile javascript into MSIL. This leaves me to wonder if IE, FF, Chrome etc are using some sort of compiler or if it's an interpreter.

Can anyone cite the specific method in which browsers run javascript?

JMax
  • 26,109
  • 12
  • 69
  • 88
  • 2
    This question is good because knowing whether code is compiled or interpreted behind the scenes can influence efficiency decisions, like whether to use for loops or explicit collection initialization: a for loop might look more readable, but contain jumps and unnecessary initialization and comparisons, so it adds overhead for the sake of readability. If a browser is compiling `for (i = 0; i < 15; i++)` into more efficient `a[0] = 1; a[1] = 2; a[3] = 3; etc.`, then knowing that will lead me to use the readable for loop instead of optimizing on my own. **TL;DR**: This is on-topic and valuable. – Michael Hoffmann Jan 18 '17 at 22:03

4 Answers4

30

In the past, Javascript was interpreted -- and nothing more.

In the past two years or so, browsers have been implementing new Javascript engines, trying to compile some portions of code, to speed Javascript up.


For more informations on what has been done for Mozilla Firefox, you should take a look at :

For more informations about Chrome's engine, you'll want to read :

And for webkit (safari) :

Not sure what has been (or is being) done on other browsers -- but I suppose the same kind of thing exists, or will exist.


And, of course, for more informations : JavaScript engine, on wikipedia.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • So basically they are all moving towards some kind of compiling. –  Mar 15 '11 at 20:17
  • Trying to -- at least for some portions of code ; done properly, the goal is to speed up the execution of JS code -- which is a great thing, considering more and more websites depend on heavy-JS. – Pascal MARTIN Mar 15 '11 at 20:18
  • nowadays browser come with js compiler which compile js and generate machine code to speed up js.......if yes then tell me the year from when browsers are using js compiler instead of interpreter. – Monojit Sarkar May 10 '17 at 14:15
1

JScript is a scripting language provided by microsoft. Its compilation is taken care by CLR. Also it can be interpreted. It have tighter integration with Visual studio.

Have a look at http://msdn.microsoft.com/en-us/library/72bd815a%28v=vs.80%29.aspx for detail Jscript description.

0

javascript scripts are usually interpreted in web browsers (not sure about chrome and V8), but here and there you can find some standalone software which can compile it more or less correctly. This language isn't as fast as many other and his speed and functionality depends on browsers engine.

Frizi
  • 2,900
  • 1
  • 19
  • 25