YUI Compressor:
- Remove comment
- Remove extra spaces
- Minor Optimization
- Identifier Replacement)
What are the minor optimizations of YUI Compressor?
- Object ["property"], if the property name is a legal JavaScript identifier (Note: A Legal JavaScript identifier -- starts with a letter, after that, you can selectively add one or more letters, numbers, or underscores (_). It is not a reserved word and will be optimized to: object. property
- {"Property": 123}. If the property name is a valid JavaScript identifier and is not a reserved word, It is optimized to {property: 123} (Note: In the object literal volume, if the property name is a valid JavaScript identifier and is not a reserved word, it is not mandatory to enclose the property name in quotation marks ).
- 'Abcd' efgh' is optimized to "abcd' efgh ".
- "Abcd" + "efgh", if the string is connected, it will be optimized to "abcdefgh" (Note: All strings in the script are connected when the YUI Compressor is used, the connector "+" has the highest efficiency and maintainability ).
For the most effective compression Optimization of JavaScript, the identifier is replaced. For example:
(function(){ function add(num1, num2) { return num1 + num2; }})();
After the identifier is replaced:
(function(){ function A(C, B) { return C+ B; }})();
Then, remove the extra space and the result is:
(function(){function A(C,B){return C+B;}})();
Replace the YUI Compressor identifier with only the function name and variable name. Which of the following cannot be replaced?
- Original Value: String, Boolean value, number, null, and undefined. Generally, strings occupy the most space, rather than numeric literal values (true, false, null, underfinded ).
- Global variables: window, document, XMLHttpRequest, and so on. Document and window are the most commonly used.
- Attribute name, such as foo. bar. The occupied space is second only to the string. The "." operator cannot be replaced, and a. B. c is more free.
- Keyword. Frequently Used keywords include var and return. Best optimization method: A function only displays the var and return keywords once.
The Optimization Methods for raw values, global variables, and attribute names are roughly the same: any literal value, global variable, or attribute name is used more than twice (including twice ), both should be replaced by local variable storage.
However, in some cases, replacement of identifiers is prohibited:
- Use the eval () function. Solution: Do not use or create a global function to encapsulate eval ().
- Use the with statement. Solution: Same as above.
- The condition annotation of JScript. The only solution: Do not use.
Because YUI Compressor is built on rhino interpreter, all the above optimizations are secure.