10. About YUI Compressor
• Remove comments
• Remove extra white space
• Identifier replacement
• Micro-optimizations
• Built on top of Rhino interpreter
– Makes all optimizations safe
11. Mozilla Rhino
• Open source JavaScript interpreter
• Written in Java
• Based on Firefox's interpreter
http://www.mozilla.org/rhino/
18. What Can't Be Replaced
• Primitive values
– strings, booleans, numbers, null, and undefined
19. Primitive Values
• Strings take up the most space
• Non-numeric literals take second-most
– true, false
– null
– undefined
• Approach: Any literal value used two or more
times should be stored in a local variable
26. What Can't Be Replaced
• Primitive values
– strings, booleans, numbers, null, and undefined
• Global variables
– window, document, XMLHttpRequest, etc.
27. Global Variables
• Most bytes:
– document
– window
• Approach: Any global variable used two or more
times should be stored into a local variable
32. What Can't Be Replaced
• Primitive values
– strings, booleans, numbers, null, and undefined
• Global variables
– window, document, XMLHttpRequest, etc.
• Property names
– foo.bar
33. Property Names
• Next to repeated strings, biggest source of extra
bytes
• Anything to the right of a dot cannot be replaced
• Makes a.b.c even more expensive
• Approach: Any property used two or more times
should be stored into a local variable
48. Preventing Identifier Replacement
• Use of eval() function
– Solution #1: Don't use
– Solution #2: Create a global function that wraps
eval()
• Use of with statement
51. Preventing Identifier Replacement
• Use of eval() function
– Solution #1: Don't use
– Solution #2: Create a global function that wraps
eval()
• Use of with statement
– Solution #1: Don't use
– Solution #2: see Solution #1
52. Preventing Identifier Replacement
• Use of eval() function
– Solution #1: Don't use
– Solution #2: Create a global function that wraps
eval()
• Use of with statement
– Solution #1: Don't use
– Solution #2: see Solution #1
• JScript conditional comments
54. Preventing Identifier Replacement
• Use of eval() function
– Solution #1: Don't use
– Solution #2: Create a global function that wraps
eval()
• Use of with statement
– Solution #1: Don't use
– Solution #2: see Solution #1
• JScript conditional comments
– Only solution: Don't use
56. Verbose Mode
• Use -v switch to enable
• Reports issues with code related to minification:
– Undeclared variables
– Unused variables
– Functions with more than one var statement
– Use of evil features (eval(), with, conditional
comments)
59. For Optimal File Size
• Use local variables to store:
– Repeated primitive values
– Global variables
– Object properties
• Limit each function to one var and one
return
• Avoid using eval() and with()
• Heed YUI Compressor's advice
• Combine with HTTP compression for best savings