16

When running JS scripts in Chromes' Performance tab, I see there are three steps for JS interpretation: Parse, Compile and Evaluate. Sometimes I just see Evaluate, sometimes Compile and Evaluate and sometimes it's the whole three.

My questions are:

  • What each step actually means?
  • Why sometimes there are missing steps? (for instance, sometimes Parse is missing)
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Eliran Pe'er
  • 2,569
  • 3
  • 22
  • 33
  • 3
    Why was this downvoted? I would actually be super happy to follow this question and see a good answer. – CodeAt30 Feb 18 '18 at 10:11
  • 1
    @CodeAt30 Probably because this is a pretty basic programming question and is what should be learned when you start programming (what is a programming language, how does it work, what is compilation, what is parsing, etc.). Also the answer should easily be found by searching for things like "what is compilation". – XCS Feb 18 '18 at 10:33
  • @cristy well `compilation` and `JIT` are quite different. And yes you can find good ressources if you know what you are looking for. And why havent you flagged it then as `to broad` ? – Jonas Wilms Feb 18 '18 at 10:44
  • @JonasW. I don't think the question is broad, it's just not a specific programming question, it's just asking resources on what "compile" "parse" and "evaluate" mean, and you can easily find info about any of them if you search online. PS: I am not the one who downvoted. – XCS Feb 18 '18 at 11:05
  • 1
    @cristy then the downvoter should explain his reasoning himself, and also flag it. I dont understand unexplained downvotes. – Jonas Wilms Feb 18 '18 at 12:35
  • 2
    not that i care about the downvote, but i really don't consider javascript interpretation as "basic programming". i actually think that most JS developers don't know enough about what the browser actually does with their code, and i feel like i know few stuff about it (ASTs, etc), but still have lots of holes in my knowledge, which is why i'm asking questions on SO. I obviously know about compiling in general, just not sure what the term means in this context, because this is not "standard" compiling. anyway, thanks everyone – Eliran Pe'er Feb 18 '18 at 12:51

1 Answers1

12

Parse:

The js engine goes over the code, determines all the different scopes, variable declarations etc. and sorts them. At this step also hoisting happens. Basically your plain text sourcecode is turned into an Abstract Syntax Tree (AST)

Compile:

Chromes V8 uses JIT compiling, that means that some parts of the js code are transfered into bytecode (that runs directly on your processor without any layer of abstraction inbetween). This increases performance. Sometimes it may decide to run the code directly without compiling it, e.g. if compiling it takes longer than actually running it unoptimized so there would be no benefit whatsoever.

Evaluating:

The code is run.

Read on:

How V8 optimizes

Bytecode vs. Running directly

All together

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thanks! but then again, why some scripts has `Parse` phase and some doesn't? (when looking via Chromes' Performance tab) it seems like a critical phase that should be taken for every script. – Eliran Pe'er Feb 18 '18 at 12:53
  • 1
    @EliranPe'er Well, my assumption is that if the script was already parsed and did not change the AST or compiled bytecode is cached and re-used instead of re-parsed. Again, it depends on the JavaScript engine being used, you should look into V8 or spidermonkey to see how they work. – XCS Feb 18 '18 at 12:54
  • @eliran i actually hoped that someone else could clarify this. I assume the same as cristy, but well *assume*... – Jonas Wilms Feb 18 '18 at 13:04
  • i don't think it's a cache issue - the script is being loaded once and not sure how it could be cached, and for other scripts i get `Parse` phase even after few tries (so it should've been cached) – Eliran Pe'er Feb 18 '18 at 13:06