Will Javascript compile or two pass interpretation?

I am a recognized novice JavaScript programmer and try to learn more. Therefore, I am asking you for help with this easy question :). The O'Reilly book that I am reading continues to refer to the compilation time of the JavaScript code. My knowledge of functional programming (circuitry and the like) tells me that JavaScript is actually interpreted by the browser, most likely it takes two passes through JavaScript.

Am I wrong in my assessment? Or the compilation time to which the book refers, in fact, only to the first pass of the interpreter, similar to how Perl or Python functions? Thanks!

+5
source share
3 answers

It depends on the browser. Take a look at WebKit SquirrelFish Extreme and Google V8 to find out what's in the fastest light, and have a look at Mozilla JaegerMonkey for this implementation.

AFIAK V8 and SFX are JIT, so they compile JS code in native. JaegerMonkey and TraceMonkey combine in Firefox to form a system in which, if the code was faster to track, TraceMonkey executes it, and if the code is faster, JaegerMonkey compiles it like SFX.

+11
source

Do you have a suggestion that you could quote to help with the context?

Javascript is compiled in a browser (it is sent to the browser as a simple source). But it only compiles as it loads. Therefore, if you have a script tag followed by a div tag, followed by a script tag, it will load these things sequentially. The browser will stop loading the entire page (it still loads resources, it just does not load HTML) until your script has been loaded (this is because the script may have a document.write document).

<script> var someVariable = 'hello world'; alert(document.getElementById('someid')); //alerts undefined </script> <div id='someid'></div> <script> alert(document.getElementById('someid')); //alerts 'someid' alert(someVariable); //alerts 'hello world' </script> 
0
source

There is read time and runtime in JS (as I like to think about it, since it is not compiled, but interpreted). O'Reilly's book seems to use compile time as a synonym for read time.

Reading time is when the engine reads all the code and evaluates everything in the global area. This usually sets hooks on events that will trigger code execution.

Runtime is everything else.

0
source

All Articles