Data Access---High-performance JavaScript reading notes (2)

Source: Internet
Author: User
Tags closure try catch

For any programming language, the location of the data store is related to the speed of access!

The direct amounts in JavaScript include string strings, number numbers, Boolean Boolean, object objects, array arrays, function functions, regular expressions regular expression, null value NULL, Array undefined not defined. The array entry is accessed through the numeric index of the array, and the object is indexed by the string to access its members (here, incidentally, because array items are indexed by numbers and object members are indexed by strings, this is why accessing object members is slower than accessing array items).

The sum is that direct and local variables are accessed much faster than array items and object members, so we should try to use direct and local variables to limit the use of array items and object members.

Both JavaScript prototype chains and closures know that in JavaScript each function is actually an object, and within each function there is a scope property that contains a collection of objects in the scope that was created . So the fact that the internal scope property is the scope of the function. When the function is run, a runtime context is established (the runtime context is actually the environment where the function runs, and for each run of the function, the runtime context is unique, and the run-time context of the function execution is destroyed, i.e. running the same function multiple times creates multiple run-time contexts).

When the function runtime context is determined, its scope is initialized (scope initialization includes function arguments, function internal variables, this), and after the scope is initialized, it is pushed into the front of the scope chain as an activation object. (In fact, this is the principle of the prototype chain, JS is through the prototype chain to implement inheritance, through the closure to implement the public and private members), which is why access to global variables is the slowest, because the global variable it is in the run-time context of the last position of the scope chain.

1. If you need to access global variables multiple times, try to save it with local variables (I think this rule applies in which programming language)

2, as little as possible to change the scope chain

    • Using the WITH
    • Try Catch

I learned that there are only two ways to change the scope chain in JavaScript (1) using the WITH Expression 2) by catching the exception try catch to implement

But with a performance-affecting expression that everyone hates, because we can completely replace it by using a local variable (because with the principle that it changes the scope chain and needs to save a lot of information to ensure that the scope chain is restored before the current operation is completed), This has a noticeable effect on performance)

A catch clause in a try catch can also change the scope chain. When an error occurs in a try block, the program automatically goes into the catch block and pushes the exception object into a Mutable object in the front of the scope chain, that is, in the catch block, all the local variables of the function are placed in the second scope chain object, but after the catch clause is executed, The scope chain is returned to its original state. The catch clause should be minimized to ensure code performance, and if you know the concept of error is high, we should try to fix the error instead of using try Catch

3, as little as possible to use closures

It is well known that the most powerful thing about closures is allowing functions to use data outside the local scope.

It is known that when a function is completed, its runtime context is destroyed and it cannot be destroyed when a closure is involved, because it may also need to be referenced by a function, which virtually requires more memory overhead.

4. Access speed is related to member nesting depth (the deeper the member nesting, the slower the access speed)

Location.href faster than Window.location.href

Window.location.href faster than window.location.href.toString ()

5, the value of the cache variable (in fact, if you need to access global variables or multiple access to the depth of the object members, using local variables to save it for later use)

This is my view of high-performance JavaScript a summary, I hope the front-end interested in the joint discussion with me!

Data Access---High-performance JavaScript reading notes (2)

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.