UglifyJS
UglifyJS is a JavaScript compressor/minifier written in JavaScript. It also contains tools that allow one to automate working with JavaScript code:
- A parser which produces an abstract syntax tree (AST) from JavaScript code.
- A code generator which outputs JavaScript code from an AST, also providing the option to get a source map.
- A compressor (optimizer) — it uses the transformer API to optimize an AST into a smaller one.
- A mangler — reduce names of local variables to (usually) single-letters.
- A scope analyzer, which is a tool that augments the AST with information about where variables are defined/referenced etc.
- A tree walker — a simple API allowing you to do something on every node in the AST.
- A tree transformer — another API intended to transform the tree.
All the above utilities and APIs are defined in ~6500 lines of code (except for the effective generation of the source-map, which is handled by the source-map module). Compared to alternatives, UglifyJS is pretty small.
These pages document the internals of UglifyJS. For a documentation of the command-line options please see the README.
Installing (for NodeJS)
You can install UglifyJS from NPM:
npm install uglify-js
but you can also use the Git repository if you feel adventurous:
git clone git://github.com/mishoo/UglifyJS2.git cd UglifyJS2 npm link .
Building for the browser
Once you installed it for NodeJS, UglifyJS2 provides a quick way to build itself for the browser:
uglifyjs --self -c -m -o /tmp/uglifyjs.js
Now you have it in /tmp/uglifyjs.js and can include that single file in a browser. If you need source maps you'll also have to include the Mozilla source-map module. details to be clarified
Exported API
UglifyJS is written in “classic style”, so to say — it's not using the CommonJS module system (I find it more of an annoyance than of real help, especially for prototyping/development). To load it in NodeJS there's a single file that needs to be require-d: tools/node.js. If you installed it with npm install or npm link as mentioned above, then this file is loaded when you say var UglifyJS = require("uglify-js2")
. This single file will load all the others in a separate execution context, so you should not worry about messing your global environment, and it returns a single object where all the global definitions in UglifyJS are exported.
Stats
Here are some stats showing where UglifyJS2 stands compared to other minifiers. Show charts.
File | Orig. | Compressor | Min. | Gzip | Time |
dojo.js.uncompressed.js | 598715 | ||||
UglifyJS2 | 114600 | 41344 | 1.696s | ||
UglifyJS1 | 117356 | 42001 | 1.633s | ||
Closure | 114148 | 41492 | 8.104s | ||
Esmangle | 119048 | 43120 | 5.586s | ||
esprima.js | 126268 | ||||
UglifyJS2 | 46672 | 13105 | 0.696s | ||
UglifyJS1 | 46384 | 13149 | 0.594s | ||
Closure | 45669 | 12982 | 4.121s | ||
Esmangle | 46040 | 13199 | 2.620s | ||
jquery-1.8.2.js | 266882 | ||||
UglifyJS2 | 92387 | 33146 | 1.604s | ||
UglifyJS1 | 93381 | 33288 | 1.539s | ||
Closure | 93557 | 33781 | 6.085s | ||
Esmangle | 93159 | 34054 | 4.444s | ||
modernizr.custom.15975.js | 29277 | ||||
UglifyJS2 | 14435 | 5854 | 0.366s | ||
UglifyJS1 | 14505 | 5869 | 0.262s | ||
Closure | 14401 | 5871 | 2.795s | ||
Esmangle | 14465 | 5909 | 0.592s | ||
mootools-core-1.4.5-full-nocompat.js | 113999 | ||||
UglifyJS2 | 83868 | 26551 | 1.079s | ||
UglifyJS1 | 84465 | 26624 | 1.095s | ||
Closure | 83555 | 26834 | 5.242s | ||
Esmangle | 84709 | 27079 | 2.671s | ||
processing-1.4.1.js | 421496 | ||||
UglifyJS2 | 208587 | 61218 | 3.461s | ||
UglifyJS1 | 208675 | 61162 | 4.003s | ||
Closure | 205118 | 61056 | 6.907s | ||
Esmangle | 213476 | 62391 | 32.281s | ||
prototype.js | 180829 | ||||
UglifyJS2 | 91656 | 28354 | 1.158s | ||
UglifyJS1 | 93311 | 28516 | 1.086s | ||
Closure | 89953 | 28190 | 5.773s | ||
Esmangle | 92981 | 29033 | 3.236s | ||
raphael.js | 221137 | ||||
UglifyJS2 | 88481 | 30831 | 1.519s | ||
UglifyJS1 | 89360 | 31022 | 1.217s | ||
Closure | 88411 | 31058 | 6.218s | ||
Esmangle | 89453 | 31942 | 3.911s | ||
underscore.js | 40966 | ||||
UglifyJS2 | 13292 | 4526 | 0.382s | ||
UglifyJS1 | 13400 | 4534 | 0.343s | ||
Closure | 13391 | 4626 | 2.496s | ||
Esmangle | 13525 | 4767 | 0.655s |