Js optimization experience

Source: Internet
Author: User

These are some of my experiences in code writing. I have summarized them and I want to share them with you. I just wrote them when I think of them.

1. Use local variables to avoid using global variables
For example

 

Change

 

The advantage of local variables is that it reduces the scope chain search.
I suggest using local variables if there are two references.

2. Avoid using with (everyone on Earth knows this)

The reason I understand is that with will create its own scope, which prolongs the original scope chain and slows down the code executed in the with block. It seems that the code is saved in writing, in fact, access becomes longer and more complex, and performance decreases.
Example
Use

 

In fact, they can all be written

 

3. Traverse nodelist

Generally

 

(Note: This method can be used in nodelist. If you use array, there may be a problem. If there is a 0 in the array, the latter is null or something will be blind)

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

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.