1. Call Stack
6888 ワード
Reference
Articles
Definition
Mechanism or structure which interpreter keeps track of functions that are called
Script calls function
interpreter adds function's execution context to Call Stack, starts executing the function
function execution is finished
interpreter removes function's execution context from Call Stack, resumes execution where it left of
stacks takes up more space than assigned
"stack overflow"
What is execution context?
Execution context is an abstract concept of environment where code is executed. Here is my guide to execution context.
Example
function greeting() {
sayHi(); //
}
function sayHi() {
return 'Hi';
}
greeting(); //
When code reaches to
greeting();
, greeting function's execution context is added to Call Stack.Execute all lines in greeting function. When code reaches to
sayHi();
, sayHi function's execution context is added to Call Stack.Execute all lines in sayHi function. When it's over, return execution to the line that invoked sayHi function, continue executing rest in greeting function. Remove sayHi function's execution context from Call Stack.
Execute all lines in greeting function. When it's over, return execution to the line that invoked greeting function, execute rest of the code. Remove greeting function's execution context from Call Stack.
Features of Call Stack
As you see the later one that is pushed, gets out first. For example, sayHi function has been added later than greeting function, but it has been removed first. We call it LIFO(Last in First out).
Also, Call Stack is for storing data temporarily. As execution of functions are finished, call stack becomes empty.
Additionally, all of execution contexts are added in order and executed in order. So we say the Call Stack is synchronous.
Javascript Engine
Definition
program that executes Javascript code. V8 Engine of Chrome, node.js, electron is one of JS engine. V8 Engine is high-performance, open source Javascript & Web Assembly engine.
JS Engine consists of Memory Heap and Call Stack. We have learned about Call Stack, so what is memory heap?
Memory Heap
Memory Heap is unstructured memory used for memory allocation of variables and objects.
JS Runtime
Definition
Environment where Javascript program is executed
Why do we need Concurrency?
When Call Stack has remained functions to execute, browser can't actually do anything, which we call it blocked. So,
Web APIs
Handle async events like DOM events, http requests, setTImeout, etc. It is created by browser, not JS engine. Web API pushes the callback onto Callback Queue when it's done executing.
Callback Queue
list of messages to be processed and associated to callback functions
Event Loops
So who chooses when the functions in Callback Queue to be executed? Event Loops does. Event Loops both Call Stack and Callback Queue and pushes the first thing on Queue to Stack when Stack is completely empty.
Reference
この問題について(1. Call Stack), 我々は、より多くの情報をここで見つけました https://velog.io/@gtfo/1.-Call-Stackテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol