Increase JavaScript speed-starting from scope access

Source: Internet
Author: User

In general, the storage location of data is related to the speed of data retrieval during code execution, and the optimal reading and writing performance of the program can be obtained by changing the data storage location.

Accessing array elements and object members in JS is more expensive than strings, numbers, booleans, and local variables defined by the developer using the keyword var.

The first thing to clarify the scope of the concept is to refer to variables and functions of the accessible scope.

Each object in JS has programmatic access to the properties and internal properties that cannot be accessed programmatically but only through the JS engine .

1. One of the internal properties is [[Scope]], which contains a collection of scoped objects when the function is created, and a collection of variables that the function can access, which is also known as the scope chain of the function.

When an object is created, the function can access which variables which data are recorded in this property. Includes global objects as well as variables defined globally, such as Window,navigator and document. and variables that can be accessed inside functions such as function parameters.

2. The second is that when the function executes, another internal object is created, called the execution Environment object. An execution environment defines the environment at which a function executes.

It has two main features: the execution environment for each execution of the function is unique, and multiple calls to the same function create several execution environment objects;

The relationship between 1 and 2 is that each execution environment has its own scope chain, which is used to parse the identifier, and when the execution Environment object is created (that is, when the function executes), its scope chain object is initialized to the object in the [[Scope]] property of the current function.

The order in which they appear in the function is copied into the scope chain of the execution environment. When the function executes, it creates an active object that includes the formal parameters of the function and the arguments and functions declared internally, and the queue is cut to the front of the scope chain.

During the execution of a function, the identifier lookup process is performed once for each variable encountered. Finds an identifier with the same name starting from the scope chain of the execution environment object. Starting from the scope chain head, the active object that starts at the top of the queue and then the lookup order of the function's scope chain object.

Summary: The deeper the location of an identifier, the slower it reads and writes. The function reads and writes local variables fastest, reading global variables is the slowest . PS can cache global variables into local variables, speed up variable reading speed!

Increase JavaScript speed-starting from scope access

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.