2

I'm trying to better understand how JS runs under the hood.

So far I am seeing that a web browser will download your JS code and immediately place it into a JS engine such as Google's V8. Within the engine, there are the two components known as the heap and the call stack. The heap is used for memory allocation and the call stack is used execute the functions.

So is it when a function is executed on the call stack that it then parses the code, turn it into an AST, which goes into an interpreter, and finally into a complier?

For more context, here are charts that I am seeing explaining this, but I still don't understand where they tie into each other.

enter image description here

enter image description here

bsmith
  • 103
  • 3
  • 10
  • 3
    Would [this other answer](https://stackoverflow.com/q/59807938/697630) help? – Edwin Dalorzo Apr 03 '21 at 16:02
  • You don't need to know anything about the second image to understand the concepts in the first and working with javascript – charlietfl Apr 03 '21 at 16:11
  • @EdwinDalorzo sort of, I'll need to research it more, but it sounds like it starts to go through the process in the second image at runtime. So does that mean the web apis and call back queue are dealing with machine code? – bsmith Apr 03 '21 at 16:21
  • 1
    No, in general the code is parsed immediately when the script is downloaded. To create a function, the code must already be parsed; it cannot be executed before that. (That said, modern JIT compilers can delay some aspects of the parsing until actual interpretation or optimisation, but syntax errors still must be detected immediately). – Bergi Apr 03 '21 at 19:29

0 Answers0