JavaScript has a concurrency model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks.This model is quite different from models in other languages like C and Java.
Run Time Concepts
Stack
Function calls form a stack of frames.
1 | function foo(b) { |
Copy to Clipboard
Order of operations:
- When calling
bar
, a first frame is created containing references tobar
‘s arguments and local variables. - When
bar
callsfoo
, a second frame is created and pushed on top of the first one, containing references tofoo
‘s arguments and local variables. - When
foo
returns, the top frame element is popped out of the stack (leaving onlybar
‘s call frame). - When
bar
returns, the stack is empty.
Note that the arguments and local variables may continue to exist, as they are stored outside the stack — so they can be accessed by any nested functions long after their outer function has returned
Heap
Objects are allocated in a heap which is just a name to denote a large (mostly unstructured) region of memory.
Queue
A JavaScript runtime uses a message queue, which is a list of messages to be processed. Each message has an associated function that gets called to handle the message.
At some point during the event loop, the runtime starts handling the messages on the queue, starting with the oldest one. To do so, the message is removed from the queue and its corresponding function is called with the message as an input parameter. As always, calling a function creates a new stack frame for that function’s use.
The processing of functions continues until the stack is once again empty. Then, the event loop will process the next message in the queue (if there is one),so some of event loop’s task is clear queue & stack.
Event loop
The event loop got its name because of how it’s usually implemented, which usually resembles:
1 | while (queue.waitForMessage()) { |
Copy to Clipboard
queue.waitForMessage()
waits synchronously for a message to arrive (if one is not already available and waiting to be handled).
“Run-to-completion”
Each message is processed completely before any other message is processed.
This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it may be stopped at any point by the runtime system to run some other code in another thread.
A downside of this model is that if a message takes too long to complete, the web application is unable to process user interactions like click or scroll. The browser mitigates this with the “a script is taking too long to run” dialog. A good practice to follow is to make message processing short and if possible cut down one message into several messages.
Adding messages
In web browsers, messages are added anytime an event occurs and there is an event listener attached to it. If there is no listener, the event is lost. So a click on an element with a click event handler will add a message—likewise with any other event.
Several runtimes communicating together
A web worker or a cross-origin iframe
has its own stack, heap, and message queue. Two distinct runtimes can only communicate through sending messages via the postMessage
method. This method adds a message to the other runtime if the latter listens to message
events.
Never blocking
A very interesting property of the event loop model is that JavaScript, unlike a lot of other languages, never blocks. Handling I/O is typically performed via events and callbacks, so when the application is waiting for an IndexedDB query to return or an XHR request to return, it can still process other things like user input.
Ref:https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop