Optimize Your JS

Source: Internet
Author: User

Over the past few days, I have readProgramDesign, Summary of JavaScript Optimization
1. compress the volume and shorten the download time.
This is the most basic and effective one. Remember a key number 1160, which is the number of bytes that can be placed in a single TCP/IP package.
1. delete comments
2. Delete tabs and spaces
3. Delete all line breaks.
4. Replace the variable name

For example, function fun (username, userage) {alert (username + userage );}
Replace with: function fun (a1, a2) {alert (a + B );}

5. Replace boolean values

For example, replace 1 with true, and replace 0 with false.

6. Shorten the negative detection

For example: if (name = NULL ){}
Replace with: If (! Name ){}

7. Use array and object literal

For example, VAR test = new array;
Replace with: var test = [];
VaR test = new object;
Replace with: var test = {};

Note: In general, the optimized JS is only available for use. In the development stage, an original version is required to facilitate reading (just like JQ)

Ii. Shorten the execution time.
Some aspects also apply to other languages.
1. Use local variables whenever possible
2. Avoid with statements as much as possible
3. Obtain a value repeatedly and assign the value to another variable.

For example: for (VAR I = 0; I <test. length; I ++ ){}
Replace with: var testlen = test. length;
For (VAR I = 0; I <testlen; I ++ ){}

4. When the if statement and multiple else statements are used, the more likely they are, the more likely they are to be.
5. try to reduce the number of else if statements and arrange the conditions in a binary tree.
6. Try to replace the if
7. You can use the array and join () method links of the stringbuffer object.
8. The built-in method is preferred.
9. Define multiple variables at a time
10. Saving Dom operations

For example, to add a node to the DOM tree, And a contains other subnodes, try to add all subnodes to a first, and then add a to the DOM tree.

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.