How the Yui compressor component compresses Javascript

Source: Internet
Author: User

 

Yui Compressor
The compressed javascript content includes:

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

  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, and 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 that is used more than twice (including twice) 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 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/

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.