JavaScript concurrency model and event loops

Source: Internet
Author: User
Tags empty end execution functions garbage collection variables variable types of functions

JavaScript's "Concurrency Model" is based on the event loop. This concurrency model differs from Java multithreading, and JavaScript concurrency is single-threaded.

I simply drew a JavaScript execution diagram, and we went through the diagram to analyze it.

Stack

Execution environment stack used for function calls

When the JS method is invoked, it enters an execution environment (execution context), and if another method is invoked (or recursively called itself), a new execution environment is created, and the execution of the code enters into the new execution environment. The function call returns the Time to revert to the original execution environment. Thus, the code execution process has formed an execution environment stack, the River Lake person "stack";

Execution Environment

The execution context is an abstract concept defined by ECMA, and all JavaScript code is executed in the execution contexts execution environment.

The global execution environment is the outermost execution environment. In a Web browser, the global execution environment is considered a Window object. Therefore, all global variables and functions are created as properties and methods of the Window object.

Global code (code executed in inline, usually including JS files, HTML pages, loaded) executes in the global execution environment. Each method call has an execution environment associated with it.

After all the code in an execution environment has finished executing, the execution environment is destroyed, and all the variables and function definitions saved in it are destroyed. The global execution environment is not destroyed until the page is closed.

Each function has its own execution environment, and when the execution flow enters a function, the execution environment of the function is pushed into the environment stack. And after the function executes, the stack

The environment stack is ejected and the control is returned to the previous execution environment. The execution flow in JS is controlled by this convenient mechanism.

When code executes in an execution environment, a scope chain (scope chain) is created for the variable object.

The purpose of the scope chain is to ensure that the current execution environment can get the variables and functions that are available in an orderly (not clear) access.

The front-end of the scope chain is always the variable object of the execution environment in which the currently executing code resides.

That is, the current scope is not, the outer scope is in the pocket.

The execution environment is created in an orderly order to do some work.

    1. First, in the execution environment of the method, the activation active object is created. The active object has another set of implementation mechanisms. Can be considered an object, but very special, no prototype, cannot be referenced directly in code.
    2. Next, when the method call creates the execution environment, the argument object (the class array object, which contains the incoming arguments, Length,callee) is created, and the active object has a "argument" attribute with the same name to reference the argument object.
    3. Next, the execution environment is assigned a scope. A scope consists of a list (or chain) of hosted objects. The contemporary code executes in an execution environment, creating a scope chain (scope chain) of the variable object. The front-end of the scope chain is always the variable object in the execution environment of the currently executing code ( And the activation activity object mentioned above is an object). If the environment is a function (the JavaScript environment has only two types of functions and global environments), its active object is used as a variable object, and the active object contains only one arguments object at the beginning.

The next variable object in the scope chain comes from the outer environment of the current code. And so on, until it reaches the outermost global execution environment, which constitutes a bottom-up chain of scopes. The variable object of the global execution environment is always the last object in the scope chain.

Identifier resolution is the process of searching for identifiers along the scope chain. The search process always starts at the very end of the scope chain, and then the main step backwards.

When entering the execution environment

When the execution environment is entered (the code is about to be executed), the variable object, which is the active object, already contains the following properties:

    1. All parameters of the function, consisting of the name and corresponding value of the variable object's properties.
    2. All the child function declarations within the function that contains the execution environment are the properties of the variable object that are composed of the name and corresponding values (function object functions objects). If a property with the same name already exists within the variable object, then all the variable declarations are replaced.
    3. A property of a variable object that consists of a name and a corresponding value (undefined) that, if the variable name is the same as a formal argument or function that has already been declared, does not interfere with such a property that already exists.

It should be explained that each execution environment has a This,this value depending on the caller and the type of code being executed, the this value is determined as it enters the execution environment. Values are associated with the execution environment and cannot be modified while the execution environment is running.

This is an attribute in the execution context.

In global code, this is always the global object itself.

In a normal function call, this is provided by the caller of the function, which is provided by the parent execution environment of the called function. This value depends on how the function is called.

Heap

A heap is a network of objects that are interconnected. In mathematical terms, it is "picture". The diagram consists of the nodes and the edges between them. Both nodes and edges are marked: the node (object) is marked with the name of the object constructor, and the edge is marked by the property name.

The sequence of edges from one object to another is called the path. Usually we are only interested in simple paths that do not repeat through the same node two times.

We call the garbage collector root node to a specified object's path called retaining path. If no such path exists, the object is called unreachable (unreachable) and should be disposed of during garbage collection.

Queue

In a Web browser, when an event occurs, if the event has a corresponding listener, the message is added to the message queue in a timely manner, and if there is no listener, the event is lost.

The JavaScript runtime is accompanied by a pending message (that is, a task) queue, each message is associated with a handler function, and when the function's execution stack is empty, that is, there is currently no function being executed, the queue picks one out to handle it. The process includes calling related functions ( Do not worry about the scope of the handler function, because in JavaScript, the scope is a lexical scope, which is determined by the method at the time of definition, regardless of the call. The scope chain was created earlier than the method call, and we can use the closure when the processing is complete, if the stack is empty, Again, try to select a handled event or task from the queue. It's easy to see how Setinterval,settimeout executes asynchronously.

This, is the event loop, the events loop.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.