How the V8 engine works
How the V8 engine works
JavaScript is a single-threaded scripting language. This means it can only do 1 thing at a time.
It has 1 call stack. An array with 2 operations: push (adds an element to the top) & pop (removes an element from the top). “Last In, First Out” protocol (LIFO).
When the engine executes a script, it pushes the global execution context, main(), into the call stack. Items in the call stack are called stack frames.
The engine executes code synchronously. Top to bottom, line by line.
When a function is invoked, its execution context gets pushed into the call stack.
When the function returns (or there are no more lines to execute), it gets popped out of the call stack.
The engine then executes the next line.
The script will stop when the call stack is empty.
function func3() {
console.log("3 invoked")
}
function func2() {
console.log("2 invoked")
func3()
}
function func1() {
console.log("1 invoked")
}
func1()
func2()
main()
func1()
func2()
func3()
Purpose: to compile JavaScript
Implements ECMAScript as specified in
ECMA-262
Open source
Developed by Google
Written in C++
Runs inside a hosting environment:
- web browser
- a server (Node.js)
Used in Chromium based browsers:
- Google Chrome
- Microsoft Edge