Data access, performance considerations-high-performance Javascript

Source: Internet
Author: User

Pay attention to the following points on the basis of understanding the scope chain:

1. Reading and Writing local variables in a function is always the fastest, while reading and writing global variables are usually the slowest. Remember that global variables always exist at the end of the context scope chain at runtime.

Rule of thumb: If a cross-scope value is referenced more than once in a function, it is saved to a local variable.

2. dynamic scope exists only inCodeDuring execution, it cannot be detected through static analysis. Several Keywords of static scope will be generated: With, try catch, and eval. dynamic scope is recommended only when necessary.

Try catch is generally within the catch code block, and all the local variables of the function will be placed in the second scope chain object.

Rule of thumb: You can minimize the impact of catch clauses on performance through simplified code. A good mode is to delegate errors to a function for processing.

3. Closure: If you need to frequently access a large number of cross-scope identifiers, each access will result in performance loss.

Rule of thumb: store common exaggerated variables in local variables and then directly access local variables.

Pay attention to these small details when writing code to improve code performance.

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.