the bigges thing is reading our code and executing it
MEMORY HEAP - we need a place to store and write information (ie variables, ...)
- all programs are just read and write operations
- memory allocation
- watch out for MEMORY LEAKS (when garbage collection fails)
CALL STACK - a place to run our code in order
- keeps track of where we're at in our code in execution
- watch out for STACK OVERFLOW
Single Threaded
only one instruction at a time - 1 call stack
no instructions in parallel
what if we have an instruction that takes a while to resolve
JS Runtime
we also have JS RUNTIME on top of the CALL STACK in browsers
if we have an instruction that runs for a while, we can't continue on in our CALL STACK
JS RUNTIME is the browser-WEB API that runs in the background - WEB API is the WINDOW OBJECT
So if CALL STACK sees async code (ie event listeners, HTTP request, etc) it goes, "this is not for me, send it to WEB API)
and it continues executing
When async code is done WEB API sends it to CALLBACK QUEUE
PARSER - lexical analysis; breaks the code into tokens
AST - abstract syntax tree
INTERPRETER - JS is mostly interpreted
- translates and reads the files line by line
- no conversion to lower language first; goes straight to running the code line by line
- perfect for JS on the fact that the program has to be user friendly on the web
- it will get slower and slower though; ie it always has to iterate a loop whereas compiler will optimize this
COMPILER - goes through the source code once to understand and optimize it (by compiling it to another programming language)
- takes a little bit more time to start up because of compiling but optimizes code, ie replacing a function with just its return
JIT COMPILER - combination of both
- the INTERPRETER part will run the code line by line ( bytecode)
- a PROFILER watches the INTERPRETER and does optimization on it simulataneously (passes the problematic code to COMPILER)
- COMPILER replaces parts of the bytecode => OPTIMIZED CODE
Optimized Code
Inline Caching - replacing codes with just the 'results' of it, so any instance of it will be optimized
Hidden Classes - instantiate your classes in order
functionAnimal(x,y){this.x=xthis.y=y}constobj1=newAnimal(1,2)constobj2=newAnimal(3,4)obj1.a=30// compiler will be slower here because of ordersobj1.b=100obj2.b=30obj2.a=100
write codes that are predictable to humans and to the computer