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".
- "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:
Copy the code as follows:
(function() { function Add (NUM1, num2) { return num1 + num2; }}) ();
After the generic identifier is replaced:
Copy the code as follows:
(function() { function A (c, B) { return C + b;< c19/>}) ();
And then remove the extra space and end up with:
The code is as follows:
(function() {function A (c,b) {return
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.
- Property name: For example: Foo.bar. Occupy the space behind the string, the "." operator cannot be substituted, and the A.B.C is more space-consuming.
- Keywords: frequently overused keywords 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 Compression JavaScript principle-"reprint"