๐๐ฆ ๐ฅ๐๐ป๐๐ถ๐บ๐ฒ ๐๐ ๐ฝ๐น๐ฎ๐ถ๐ป๐ฒ๐ฑ
JavaScript uses JIT compilation. It works in steps.
- Parser turns code into a tree.
- Interpreter turns the tree into bytecode.
- Profiler finds hot code paths.
- JIT Compiler turns hot code into machine code.
- Engine reverts to bytecode if types change.
JS is single threaded. It runs one instruction at a time on one call stack. It has no parallel execution.
The engine creates an Execution Context for your code.
- Global Context starts with your script.
- Function Context starts when you call a function.
Execution has two phases.
- Memory Creation. The engine stores variables.
- Code Execution. The engine runs code line by line.
The call stack uses LIFO. Last In, First Out. If you run an infinite loop, your browser freezes. The stack never empties.
Hoisting moves declarations to the top.
- var is initialized as undefined.
- let and const are not initialized. This creates the Temporal Dead Zone.
JS does not handle timers or network calls. The host environment does this. The browser or Node.js manages these tasks.
The Event Loop handles callbacks.
- Microtasks: Promise callbacks. High priority.
- Macrotasks: setTimeout and DOM events. Low priority.
The loop runs all microtasks first. Then it runs one macrotask.
Async and await suspend functions. Code after await becomes a microtask.
Source: https://dev.to/dhanushsgowda/your-last-min-js-revision-part-1-the-runtime-30di