Yui Compressor
The compressed javascript content includes:
- Remove comment
- Remove extra spaces
- Minor Optimization
- Identifier replacement)
Yui Compressor
Which minor optimizations are included?
object
[
"property"
]
If the property name is a legal JavaScript identifier (Note: A Legal JavaScript identifier -- starts with a letter and is selectively followed by one or more letters, numbers, or underscores) it is not a reserved word and will be optimized:object
.
property
{
"property"
:
123
}
If the property name is a valid JavaScript identifier and is not a reserved word, It is optimized{
property
:
123
}
(Note: 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'
, Will be optimized"abcd'efgh"
.
"abcd"
+
"efgh"
, If the string is connected, it will be optimized"abcdefgh"
(Note: All strings in the script are connected with the connector "+", which has the highest efficiency and maintainability ).
Which is the most effective compression Optimization for JavaScript?Identifier replacement
.
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;}})();
Yui Compressor
Replace identifier onlyFunction 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, and 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 that is used more than twice (including twice) 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 created in rhino Interpreter
So all the above optimization is safe.
Additional reading:
- Extreme JavaScript compression with Yui Compressor
From: http://www.planabc.net/2009/08/02/javascript-compression_with_yui_compressor/