Cover by Nicholas C. Zakas

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

JavaScript Minification

JavaScript minification is the process by which a JavaScript file is stripped of everything that does not contribute to its execution. This includes comments and unnecessary whitespace. The process typically reduces the file size by half, resulting in faster downloads, and encourages programmers to write better, more extensive inline documentation.

JSMin (http://www.crockford.com/javascript/jsmin.html), developed by Douglas Crockford, remained the standard in JavaScript minification for a long time. However, as web applications kept growing in size and complexity, many felt it was time to push JavaScript minification a step further. This is the main reason behind the development of the YUI Compressor (http://developer.yahoo.com/yui/compressor/), a tool that performs all kinds of smart operations in order to offer a higher level of compaction than other tools in a completely safe way. In addition to stripping comments and unnecessary whitespace, the YUI Compressor offers the following features:

  • Replacement of local variable names with shorter (one-, two-, or three-character) variable names, picked to optimize gzip compression downstream

  • Replacement of bracket notation with dot notation whenever possible (e.g., foo["bar"] becomes foo.bar)

  • Replacement of quoted literal property names whenever possible (e.g., {"foo":"bar"} becomes {foo:"bar"})

  • Replacement of escaped quotes in strings (e.g., 'aaa\'bbb' becomes "aaa'bbb")

  • Constant folding (e.g., "foo"+"bar" becomes ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required