High-performance javascript--data storage (brief study Note II)

Source: Internet
Author: User

Four basic data access locations in 1.JavaScript: literal, local variable, array element, object member.

General: [literal, local variable] run speed >[Array, object member]   2. The Internal property contains a collection of objects in the scope in which a function is created. This collection is called the scope chain.   3. Execute functions, create an execution environment, create an active object (that is, a function runtime variable object). so calling the same function multiple times causes multiple execution environments to be created.   4. Function Execution Processeach time a variable is encountered, it goes through the identifier parsing process, where the data is fetched or stored. This procedure searches the scope chain of the execution environment. It is this search process that affects performance.  5. Performance of identifier parsingGlobal variables always exist at the end of the execution environment scope. Local variables are first-level resolution. rule of thumb: if a cross-scope value is referenced more than once in a function, store it in a local variable. such as:
function Initui () {    var bd=document.body;     // There are multiple calls to this global object after Doucument  ///-optimized function  initui () {    var doc=  Document;         BD=doc.body;     // Store a reference to the Doucument global object in the local variable doc  }
   6. Change the scope chainIn general, the scope chain of an execution environment does not change. <1>with can temporarily change the scope chainwidth is used to create a variable for all properties of an object
function Initui () {    with(document) {        var bd=body;           }}

The scope chain of the execution environment is temporarily changed when the code executes to with. A new variable object is created, which contains all the properties of the parameter specified object. This object is pushed to the top of the scope chain, so at this time all local variables are in the second scoped chain object, so the access cost is higher.   <2>try-catchwhen an error occurs in a try statement, the execution process automatically jumps to the catch. It then pushes the exception object into a variable object and places it at the first place in the scope. Note: once the catch sub-statement is executed, the scope chain returns to its previous state.   7. Performance issues caused by closuresclosures are one of the most powerful features of JavaScript. because the closure contains a reference to the same object as the environment scope chain, the active object of the function is not destroyed, resulting in more memory overhead. focus on performance points : When you frequently access identifiers across scopes, each access brings a performance penalty.

High-performance javascript--data storage (brief study Note II)

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.