How does YUI Compressor compress JS Code?

Source: Internet
Author: User

YUI Compressor:

  1. Remove comment
  2. Remove extra spaces
  3. Minor Optimization
  4. 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?

  1. Original Value: String, Boolean value, number, null, and undefined. Generally, strings occupy the most space, rather than numeric literal values (true, false, null, underfinded ).
  2. Global variables: window, document, XMLHttpRequest, and so on. Document and window are the most commonly used.
  3. 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.
  4. 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:

  1. Use the eval () function. Solution: Do not use or create a global function to encapsulate eval ().
  2. Use the with statement. Solution: Same as above.
  3. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.