I recently wrote a jQuery plug-in. When the optimization was completed, I found that the compressed files were relatively large. I thought about those that can be modified and optimized. I found that there is a lot of room to learn about compression principles. Through this time, I compressed JavaScript with YUI Compressor. I have a deep understanding that those that can be compressed, especially those that cannot be compressed, need to be very clear, so that the plug-ins can be written to keep the file smaller and the code more compact, and during the optimization process, it will also be found that the code needs to be The improvements will be of great help in the future. I randomly found an article on the Internet to record it.
YUI Compressor compresses JavaScript content including :
Remove comments
Remove extra spaces
Subtle optimization
Identifier Replacement
What subtle optimizations does YUI Compressor include?
object["property"] , if the property name is a legal JavaScript identifier (note: a legal JavaScript identifier starts with a letter, optionally followed by one or more letters, numbers or underscores) and is not a reserved word, it will be optimized to: object.property
{"property":123}. If the property name is a legal JavaScript identifier and is not a reserved word, it will be optimized to {property:123} ( Note: In object literals, it is not mandatory to quote the property name if it is a legal JavaScript identifier and is not a reserved word).
'abcd'efgh', will be optimized to "abcd'efgh".
"abcd" "efgh", if it is a string connection, it will be optimized to "abcdefgh" (Note: All under the premise of using YUI Compressor, for the string connection in the script, use the connector " " for efficiency and highest maintainability).
The most effective compression optimization for JavaScript is identifier replacement.
For example: