YUI Compressor compresses JavaScript content including:
- Remove comments
- Remove extra spaces
- Fine tuning
- Identifier substitution (Identifier replacement)
What minor optimizations does YUI Compressor include?
- Object["Property"], if the attribute name is a valid JavaScript identifier (NOTE: A valid JavaScript identifier-starting with a letter, followed by selectively adding one or more letters, numbers, or underscores) and not reserved words, is optimized to: Object.property
- {"Property": 123}, if the attribute name is a valid JavaScript identifier and is not a reserved word, it will be optimized to {property:123} (note: In object literals, if the property name is a valid JavaScript identifier and is not a reserved word, Does not require quotation marks to enclose the attribute name.
- ' ABCD ' efgh ', will be optimized for "ABCD ' Efgh". Fanzhi County Grain Bureau
- "ABCD" + "EFGH", if the string is connected, will be optimized to "ABCDEFGH" (note: All under the premise of using YUI Compressor, for the string connection in the script, using the connector "+" the highest efficiency and maintainability).
For JavaScript, the most efficient compression optimization, when a generic identifier is replaced. Like what:
View Source print?
2 |
function add(num1, num2) { |
After the generic identifier is replaced:
View Source print?
And then remove the extra space and end up with:
View Source print?
1 |
( function (){ function A(C,B){ return C+B;}})(); |
YUI Compressor identifier substitution replaces only function names and variable names, which cannot be substituted?
- Original values: String, Boolean, number, NULL, and undefined. In general, strings occupy the most space, not numeric literals followed by (true, false,null,underfinded).
- Global variables: window, document, XMLHttpRequest, and so on. The most used is document, window.
- Attribute names, such as: Foo.bar. Occupy the space behind the string, the "." operator cannot be substituted, and the A.B.C is more space-consuming.
- Key word. Keywords that are often overused are: Var, return. Best optimization method: A function only appears once for the Var and return keywords.
The optimization of raw values, global variables, and property names is handled in much the same way: any literal value, global variable, or attribute name is used more than 2 times (including 2 times) and should be replaced with local variable storage.
But in some cases it is forbidden to replace with identifiers:
- Use the eval () function. Workaround: Do not use or create a global function to encapsulate eval ().
- Use the WITH statement. Workaround: The same method as above.
- Conditional comments for JScript. The only workaround: Do not use.
Since YUI Compressor is based on Rhino interpreter, all of the above optimizations are safe.
YUI compressor is how to compress the JS code?