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.