xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / uglify-js / README.html

28.6 KB
1<?xml version="1.0" encoding="utf-8"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml"
5lang="en" xml:lang="en">
6<head>
7<title>UglifyJS -- a JavaScript parser/compressor/beautifier</title>
8<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
9<meta name="generator" content="Org-mode"/>
10<meta name="generated" content="2011-04-04 00:07:13 EEST"/>
11<meta name="author" content="Mihai Bazon"/>
12<meta name="description" content="a JavaScript parser/compressor/beautifier in JavaScript"/>
13<meta name="keywords" content="javascript, js, parser, compiler, compressor, mangle, minify, minifier"/>
14<style type="text/css">
15 <!--/*--><![CDATA[/*><!--*/
16  html { font-family: Times, serif; font-size: 12pt; }
17  .title  { text-align: center; }
18  .todo   { color: red; }
19  .done   { color: green; }
20  .tag    { background-color: #add8e6; font-weight:normal }
21  .target { }
22  .timestamp { color: #bebebe; }
23  .timestamp-kwd { color: #5f9ea0; }
24  p.verse { margin-left: 3% }
25  pre {
26	border: 1pt solid #AEBDCC;
27	background-color: #F3F5F7;
28	padding: 5pt;
29	font-family: courier, monospace;
30        font-size: 90%;
31        overflow:auto;
32  }
33  table { border-collapse: collapse; }
34  td, th { vertical-align: top; }
35  dt { font-weight: bold; }
36  div.figure { padding: 0.5em; }
37  div.figure p { text-align: center; }
38  textarea { overflow-x: auto; }
39  .linenr { font-size:smaller }
40  .code-highlighted {background-color:#ffff00;}
41  .org-info-js_info-navigation { border-style:none; }
42  #org-info-js_console-label { font-size:10px; font-weight:bold;
43                               white-space:nowrap; }
44  .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
45                                 font-weight:bold; }
46  /*]]>*/-->
47</style>
48<link rel="stylesheet" type="text/css" href="docstyle.css" />
49<script type="text/javascript">
50<!--/*--><![CDATA[/*><!--*/
51 function CodeHighlightOn(elem, id)
52 {
53   var target = document.getElementById(id);
54   if(null != target) {
55     elem.cacheClassElem = elem.className;
56     elem.cacheClassTarget = target.className;
57     target.className = "code-highlighted";
58     elem.className   = "code-highlighted";
59   }
60 }
61 function CodeHighlightOff(elem, id)
62 {
63   var target = document.getElementById(id);
64   if(elem.cacheClassElem)
65     elem.className = elem.cacheClassElem;
66   if(elem.cacheClassTarget)
67     target.className = elem.cacheClassTarget;
68 }
69/*]]>*///-->
70</script>
71
72</head>
73<body>
74<div id="content">
75
76<h1 class="title">UglifyJS &ndash; a JavaScript parser/compressor/beautifier</h1>
77
78
79<div id="table-of-contents">
80<h2>Table of Contents</h2>
81<div id="text-table-of-contents">
82<ul>
83<li><a href="#sec-1">1 UglifyJS &mdash; a JavaScript parser/compressor/beautifier </a>
84<ul>
85<li><a href="#sec-1_1">1.1 Unsafe transformations </a>
86<ul>
87<li><a href="#sec-1_1_1">1.1.1 Calls involving the global Array constructor </a></li>
88</ul>
89</li>
90<li><a href="#sec-1_2">1.2 Install (NPM) </a></li>
91<li><a href="#sec-1_3">1.3 Install latest code from GitHub </a></li>
92<li><a href="#sec-1_4">1.4 Usage </a>
93<ul>
94<li><a href="#sec-1_4_1">1.4.1 API </a></li>
95<li><a href="#sec-1_4_2">1.4.2 Beautifier shortcoming &ndash; no more comments </a></li>
96</ul>
97</li>
98<li><a href="#sec-1_5">1.5 Compression &ndash; how good is it? </a></li>
99<li><a href="#sec-1_6">1.6 Bugs? </a></li>
100<li><a href="#sec-1_7">1.7 Links </a></li>
101<li><a href="#sec-1_8">1.8 License </a></li>
102</ul>
103</li>
104</ul>
105</div>
106</div>
107
108<div id="outline-container-1" class="outline-2">
109<h2 id="sec-1"><span class="section-number-2">1</span> UglifyJS &mdash; a JavaScript parser/compressor/beautifier </h2>
110<div class="outline-text-2" id="text-1">
111
112
113<p>
114This package implements a general-purpose JavaScript
115parser/compressor/beautifier toolkit.  It is developed on <a href="http://nodejs.org/">NodeJS</a>, but it
116should work on any JavaScript platform supporting the CommonJS module system
117(and if your platform of choice doesn't support CommonJS, you can easily
118implement it, or discard the <code>exports.*</code> lines from UglifyJS sources).
119</p>
120<p>
121The tokenizer/parser generates an abstract syntax tree from JS code.  You
122can then traverse the AST to learn more about the code, or do various
123manipulations on it.  This part is implemented in <a href="../lib/parse-js.js">parse-js.js</a> and it's a
124port to JavaScript of the excellent <a href="http://marijn.haverbeke.nl/parse-js/">parse-js</a> Common Lisp library from <a href="http://marijn.haverbeke.nl/">Marijn Haverbeke</a>.
125</p>
126<p>
127( See <a href="http://github.com/mishoo/cl-uglify-js">cl-uglify-js</a> if you're looking for the Common Lisp version of
128UglifyJS. )
129</p>
130<p>
131The second part of this package, implemented in <a href="../lib/process.js">process.js</a>, inspects and
132manipulates the AST generated by the parser to provide the following:
133</p>
134<ul>
135<li>
136ability to re-generate JavaScript code from the AST.  Optionally
137indented&mdash;you can use this if you want to “beautify” a program that has
138been compressed, so that you can inspect the source.  But you can also run
139our code generator to print out an AST without any whitespace, so you
140achieve compression as well.
141
142</li>
143<li>
144shorten variable names (usually to single characters).  Our mangler will
145analyze the code and generate proper variable names, depending on scope
146and usage, and is smart enough to deal with globals defined elsewhere, or
147with <code>eval()</code> calls or <code>with{}</code> statements.  In short, if <code>eval()</code> or
148<code>with{}</code> are used in some scope, then all variables in that scope and any
149variables in the parent scopes will remain unmangled, and any references
150to such variables remain unmangled as well.
151
152</li>
153<li>
154various small optimizations that may lead to faster code but certainly
155lead to smaller code.  Where possible, we do the following:
156
157<ul>
158<li>
159foo["bar"]  ==&gt;  foo.bar
160
161</li>
162<li>
163remove block brackets <code>{}</code>
164
165</li>
166<li>
167join consecutive var declarations:
168var a = 10; var b = 20; ==&gt; var a=10,b=20;
169
170</li>
171<li>
172resolve simple constant expressions: 1 +2 * 3 ==&gt; 7.  We only do the
173replacement if the result occupies less bytes; for example 1/3 would
174translate to 0.333333333333, so in this case we don't replace it.
175
176</li>
177<li>
178consecutive statements in blocks are merged into a sequence; in many
179cases, this leaves blocks with a single statement, so then we can remove
180the block brackets.
181
182</li>
183<li>
184various optimizations for IF statements:
185
186<ul>
187<li>
188if (foo) bar(); else baz(); ==&gt; foo?bar():baz();
189</li>
190<li>
191if (!foo) bar(); else baz(); ==&gt; foo?baz():bar();
192</li>
193<li>
194if (foo) bar(); ==&gt; foo&amp;&amp;bar();
195</li>
196<li>
197if (!foo) bar(); ==&gt; foo||bar();
198</li>
199<li>
200if (foo) return bar(); else return baz(); ==&gt; return foo?bar():baz();
201</li>
202<li>
203if (foo) return bar(); else something(); ==&gt; {if(foo)return bar();something()}
204
205</li>
206</ul>
207</li>
208<li>
209remove some unreachable code and warn about it (code that follows a
210<code>return</code>, <code>throw</code>, <code>break</code> or <code>continue</code> statement, except
211function/variable declarations).
212</li>
213</ul>
214</li>
215</ul>
216
217
218
219</div>
220
221<div id="outline-container-1_1" class="outline-3">
222<h3 id="sec-1_1"><span class="section-number-3">1.1</span> <span class="target">Unsafe transformations</span>  </h3>
223<div class="outline-text-3" id="text-1_1">
224
225
226<p>
227UglifyJS tries its best to achieve great compression while leaving the
228semantics of the code intact.  In general, if your code logic is broken by
229UglifyJS then it's a bug in UglifyJS and you should report it and I should
230fix it. :-)
231</p>
232<p>
233However, I opted to include the following potentially unsafe transformations
234as default behavior.  Discussion is welcome, if you have ideas of how to
235handle this better, or any objections to these optimizations, please let me
236know.
237</p>
238
239</div>
240
241<div id="outline-container-1_1_1" class="outline-4">
242<h4 id="sec-1_1_1"><span class="section-number-4">1.1.1</span> Calls involving the global Array constructor </h4>
243<div class="outline-text-4" id="text-1_1_1">
244
245
246<p>
247The following transformations occur:
248</p>
249
250
251
252<pre class="src src-js"><span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3, 4)  =&gt; [1,2,3,4]
253Array(a, b, c)         =&gt; [a,b,c]
254<span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(5)           =&gt; Array(5)
255<span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(a)           =&gt; Array(a)
256</pre>
257
258
259
260<p>
261These are all safe if the Array name isn't redefined.  JavaScript does allow
262one to globally redefine Array (and pretty much everything, in fact) but I
263personally don't see why would anyone do that.
264</p>
265<p>
266UglifyJS does handle the case where Array is redefined locally, or even
267globally but with a <code>function</code> or <code>var</code> declaration.  Therefore, in the
268following cases UglifyJS <b>doesn't touch</b> calls or instantiations of Array:
269</p>
270
271
272
273<pre class="src src-js"><span style="color: #b22222;">// </span><span style="color: #b22222;">case 1.  globally declared variable
274</span>  <span style="color: #a020f0;">var</span> <span style="color: #b8860b;">Array</span>;
275  <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3);
276  Array(a, b);
277
278  <span style="color: #b22222;">// </span><span style="color: #b22222;">or (can be declared later)
279</span>  <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3);
280  <span style="color: #a020f0;">var</span> <span style="color: #b8860b;">Array</span>;
281
282  <span style="color: #b22222;">// </span><span style="color: #b22222;">or (can be a function)
283</span>  <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3);
284  <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">Array</span>() { ... }
285
286<span style="color: #b22222;">// </span><span style="color: #b22222;">case 2.  declared in a function
287</span>  (<span style="color: #a020f0;">function</span>(){
288    a = <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3);
289    b = Array(5, 6);
290    <span style="color: #a020f0;">var</span> <span style="color: #b8860b;">Array</span>;
291  })();
292
293  <span style="color: #b22222;">// </span><span style="color: #b22222;">or
294</span>  (<span style="color: #a020f0;">function</span>(<span style="color: #b8860b;">Array</span>){
295    <span style="color: #a020f0;">return</span> Array(5, 6, 7);
296  })();
297
298  <span style="color: #b22222;">// </span><span style="color: #b22222;">or
299</span>  (<span style="color: #a020f0;">function</span>(){
300    <span style="color: #a020f0;">return</span> <span style="color: #a020f0;">new</span> <span style="color: #228b22;">Array</span>(1, 2, 3, 4);
301    <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">Array</span>() { ... }
302  })();
303
304  <span style="color: #b22222;">// </span><span style="color: #b22222;">etc.
305</span></pre>
306
307
308
309</div>
310</div>
311
312</div>
313
314<div id="outline-container-1_2" class="outline-3">
315<h3 id="sec-1_2"><span class="section-number-3">1.2</span> Install (NPM) </h3>
316<div class="outline-text-3" id="text-1_2">
317
318
319<p>
320UglifyJS is now available through NPM &mdash; <code>npm install uglify-js</code> should do
321the job.
322</p>
323</div>
324
325</div>
326
327<div id="outline-container-1_3" class="outline-3">
328<h3 id="sec-1_3"><span class="section-number-3">1.3</span> Install latest code from GitHub </h3>
329<div class="outline-text-3" id="text-1_3">
330
331
332
333
334
335<pre class="src src-sh"><span style="color: #b22222;">## </span><span style="color: #b22222;">clone the repository
336</span>mkdir -p /where/you/wanna/put/it
337<span style="color: #da70d6;">cd</span> /where/you/wanna/put/it
338git clone git://github.com/mishoo/UglifyJS.git
339
340<span style="color: #b22222;">## </span><span style="color: #b22222;">make the module available to Node
341</span>mkdir -p ~/.node_libraries/
342<span style="color: #da70d6;">cd</span> ~/.node_libraries/
343ln -s /where/you/wanna/put/it/UglifyJS/uglify-js.js
344
345<span style="color: #b22222;">## </span><span style="color: #b22222;">and if you want the CLI script too:
346</span>mkdir -p ~/bin
347<span style="color: #da70d6;">cd</span> ~/bin
348ln -s /where/you/wanna/put/it/UglifyJS/bin/uglifyjs
349  <span style="color: #b22222;"># </span><span style="color: #b22222;">(then add ~/bin to your $PATH if it's not there already)
350</span></pre>
351
352
353
354</div>
355
356</div>
357
358<div id="outline-container-1_4" class="outline-3">
359<h3 id="sec-1_4"><span class="section-number-3">1.4</span> Usage </h3>
360<div class="outline-text-3" id="text-1_4">
361
362
363<p>
364There is a command-line tool that exposes the functionality of this library
365for your shell-scripting needs:
366</p>
367
368
369
370<pre class="src src-sh">uglifyjs [ options... ] [ filename ]
371</pre>
372
373
374
375<p>
376<code>filename</code> should be the last argument and should name the file from which
377to read the JavaScript code.  If you don't specify it, it will read code
378from STDIN.
379</p>
380<p>
381Supported options:
382</p>
383<ul>
384<li>
385<code>-b</code> or <code>--beautify</code> &mdash; output indented code; when passed, additional
386options control the beautifier:
387
388<ul>
389<li>
390<code>-i N</code> or <code>--indent N</code> &mdash; indentation level (number of spaces)
391
392</li>
393<li>
394<code>-q</code> or <code>--quote-keys</code> &mdash; quote keys in literal objects (by default,
395only keys that cannot be identifier names will be quotes).
396
397</li>
398</ul>
399</li>
400<li>
401<code>--ascii</code> &mdash; pass this argument to encode non-ASCII characters as
402<code>\uXXXX</code> sequences.  By default UglifyJS won't bother to do it and will
403output Unicode characters instead.  (the output is always encoded in UTF8,
404but if you pass this option you'll only get ASCII).
405
406</li>
407<li>
408<code>-nm</code> or <code>--no-mangle</code> &mdash; don't mangle variable names
409
410</li>
411<li>
412<code>-ns</code> or <code>--no-squeeze</code> &mdash; don't call <code>ast_squeeze()</code> (which does various
413optimizations that result in smaller, less readable code).
414
415</li>
416<li>
417<code>-mt</code> or <code>--mangle-toplevel</code> &mdash; mangle names in the toplevel scope too
418(by default we don't do this).
419
420</li>
421<li>
422<code>--no-seqs</code> &mdash; when <code>ast_squeeze()</code> is called (thus, unless you pass
423<code>--no-squeeze</code>) it will reduce consecutive statements in blocks into a
424sequence.  For example, "a = 10; b = 20; foo();" will be written as
425"a=10,b=20,foo();".  In various occasions, this allows us to discard the
426block brackets (since the block becomes a single statement).  This is ON
427by default because it seems safe and saves a few hundred bytes on some
428libs that I tested it on, but pass <code>--no-seqs</code> to disable it.
429
430</li>
431<li>
432<code>--no-dead-code</code> &mdash; by default, UglifyJS will remove code that is
433obviously unreachable (code that follows a <code>return</code>, <code>throw</code>, <code>break</code> or
434<code>continue</code> statement and is not a function/variable declaration).  Pass
435this option to disable this optimization.
436
437</li>
438<li>
439<code>-nc</code> or <code>--no-copyright</code> &mdash; by default, <code>uglifyjs</code> will keep the initial
440comment tokens in the generated code (assumed to be copyright information
441etc.).  If you pass this it will discard it.
442
443</li>
444<li>
445<code>-o filename</code> or <code>--output filename</code> &mdash; put the result in <code>filename</code>.  If
446this isn't given, the result goes to standard output (or see next one).
447
448</li>
449<li>
450<code>--overwrite</code> &mdash; if the code is read from a file (not from STDIN) and you
451pass <code>--overwrite</code> then the output will be written in the same file.
452
453</li>
454<li>
455<code>--ast</code> &mdash; pass this if you want to get the Abstract Syntax Tree instead
456of JavaScript as output.  Useful for debugging or learning more about the
457internals.
458
459</li>
460<li>
461<code>-v</code> or <code>--verbose</code> &mdash; output some notes on STDERR (for now just how long
462each operation takes).
463
464</li>
465<li>
466<code>--extra</code> &mdash; enable additional optimizations that have not yet been
467extensively tested.  These might, or might not, break your code.  If you
468find a bug using this option, please report a test case.
469
470</li>
471<li>
472<code>--unsafe</code> &mdash; enable other additional optimizations that are known to be
473unsafe in some contrived situations, but could still be generally useful.
474For now only this:
475
476<ul>
477<li>
478foo.toString()  ==&gt;  foo+""
479
480</li>
481</ul>
482</li>
483<li>
484<code>--max-line-len</code> (default 32K characters) &mdash; add a newline after around
48532K characters.  I've seen both FF and Chrome croak when all the code was
486on a single line of around 670K.  Pass &ndash;max-line-len 0 to disable this
487safety feature.
488
489</li>
490<li>
491<code>--reserved-names</code> &mdash; some libraries rely on certain names to be used, as
492pointed out in issue #92 and #81, so this option allow you to exclude such
493names from the mangler.  For example, to keep names <code>require</code> and <code>$super</code>
494intact you'd specify &ndash;reserved-names "require,$super".
495</li>
496</ul>
497
498
499
500</div>
501
502<div id="outline-container-1_4_1" class="outline-4">
503<h4 id="sec-1_4_1"><span class="section-number-4">1.4.1</span> API </h4>
504<div class="outline-text-4" id="text-1_4_1">
505
506
507<p>
508To use the library from JavaScript, you'd do the following (example for
509NodeJS):
510</p>
511
512
513
514<pre class="src src-js"><span style="color: #a020f0;">var</span> <span style="color: #b8860b;">jsp</span> = require(<span style="color: #bc8f8f;">"uglify-js"</span>).parser;
515<span style="color: #a020f0;">var</span> <span style="color: #b8860b;">pro</span> = require(<span style="color: #bc8f8f;">"uglify-js"</span>).uglify;
516
517<span style="color: #a020f0;">var</span> <span style="color: #b8860b;">orig_code</span> = <span style="color: #bc8f8f;">"... JS code here"</span>;
518<span style="color: #a020f0;">var</span> <span style="color: #b8860b;">ast</span> = jsp.parse(orig_code); <span style="color: #b22222;">// </span><span style="color: #b22222;">parse code and get the initial AST
519</span>ast = pro.ast_mangle(ast); <span style="color: #b22222;">// </span><span style="color: #b22222;">get a new AST with mangled names
520</span>ast = pro.ast_squeeze(ast); <span style="color: #b22222;">// </span><span style="color: #b22222;">get an AST with compression optimizations
521</span><span style="color: #a020f0;">var</span> <span style="color: #b8860b;">final_code</span> = pro.gen_code(ast); <span style="color: #b22222;">// </span><span style="color: #b22222;">compressed code here
522</span></pre>
523
524
525
526<p>
527The above performs the full compression that is possible right now.  As you
528can see, there are a sequence of steps which you can apply.  For example if
529you want compressed output but for some reason you don't want to mangle
530variable names, you would simply skip the line that calls
531<code>pro.ast_mangle(ast)</code>.
532</p>
533<p>
534Some of these functions take optional arguments.  Here's a description:
535</p>
536<ul>
537<li>
538<code>jsp.parse(code, strict_semicolons)</code> &ndash; parses JS code and returns an AST.
539<code>strict_semicolons</code> is optional and defaults to <code>false</code>.  If you pass
540<code>true</code> then the parser will throw an error when it expects a semicolon and
541it doesn't find it.  For most JS code you don't want that, but it's useful
542if you want to strictly sanitize your code.
543
544</li>
545<li>
546<code>pro.ast_mangle(ast, options)</code> &ndash; generates a new AST containing mangled
547(compressed) variable and function names.  It supports the following
548options:
549
550<ul>
551<li>
552<code>toplevel</code> &ndash; mangle toplevel names (by default we don't touch them).
553</li>
554<li>
555<code>except</code> &ndash; an array of names to exclude from compression.
556
557</li>
558</ul>
559</li>
560<li>
561<code>pro.ast_squeeze(ast, options)</code> &ndash; employs further optimizations designed
562to reduce the size of the code that <code>gen_code</code> would generate from the
563AST.  Returns a new AST.  <code>options</code> can be a hash; the supported options
564are:
565
566<ul>
567<li>
568<code>make_seqs</code> (default true) which will cause consecutive statements in a
569block to be merged using the "sequence" (comma) operator
570
571</li>
572<li>
573<code>dead_code</code> (default true) which will remove unreachable code.
574
575</li>
576</ul>
577</li>
578<li>
579<code>pro.gen_code(ast, options)</code> &ndash; generates JS code from the AST.  By
580default it's minified, but using the <code>options</code> argument you can get nicely
581formatted output.  <code>options</code> is, well, optional :-) and if you pass it it
582must be an object and supports the following properties (below you can see
583the default values):
584
585<ul>
586<li>
587<code>beautify: false</code> &ndash; pass <code>true</code> if you want indented output
588</li>
589<li>
590<code>indent_start: 0</code> (only applies when <code>beautify</code> is <code>true</code>) &ndash; initial
591indentation in spaces
592</li>
593<li>
594<code>indent_level: 4</code> (only applies when <code>beautify</code> is <code>true</code>) --
595indentation level, in spaces (pass an even number)
596</li>
597<li>
598<code>quote_keys: false</code> &ndash; if you pass <code>true</code> it will quote all keys in
599literal objects
600</li>
601<li>
602<code>space_colon: false</code> (only applies when <code>beautify</code> is <code>true</code>) &ndash; wether
603to put a space before the colon in object literals
604</li>
605<li>
606<code>ascii_only: false</code> &ndash; pass <code>true</code> if you want to encode non-ASCII
607characters as <code>\uXXXX</code>.
608</li>
609</ul>
610</li>
611</ul>
612
613
614</div>
615
616</div>
617
618<div id="outline-container-1_4_2" class="outline-4">
619<h4 id="sec-1_4_2"><span class="section-number-4">1.4.2</span> Beautifier shortcoming &ndash; no more comments </h4>
620<div class="outline-text-4" id="text-1_4_2">
621
622
623<p>
624The beautifier can be used as a general purpose indentation tool.  It's
625useful when you want to make a minified file readable.  One limitation,
626though, is that it discards all comments, so you don't really want to use it
627to reformat your code, unless you don't have, or don't care about, comments.
628</p>
629<p>
630In fact it's not the beautifier who discards comments &mdash; they are dumped at
631the parsing stage, when we build the initial AST.  Comments don't really
632make sense in the AST, and while we could add nodes for them, it would be
633inconvenient because we'd have to add special rules to ignore them at all
634the processing stages.
635</p>
636</div>
637</div>
638
639</div>
640
641<div id="outline-container-1_5" class="outline-3">
642<h3 id="sec-1_5"><span class="section-number-3">1.5</span> Compression &ndash; how good is it? </h3>
643<div class="outline-text-3" id="text-1_5">
644
645
646<p>
647(XXX: this is somewhat outdated.  On the jQuery source code we beat Closure
648by 168 bytes (560 after gzip) and by many seconds.)
649</p>
650<p>
651There are a few popular JS minifiers nowadays &ndash; the two most well known
652being the GoogleClosure (GCL) compiler and the YUI compressor.  For some
653reason they are both written in Java.  I didn't really hope to beat any of
654them, but finally I did &ndash; UglifyJS compresses better than the YUI
655compressor, and safer than GoogleClosure.
656</p>
657<p>
658I tested it on two big libraries.  <a href="http://www.dynarchlib.com/">DynarchLIB</a> is my own, and it's big enough
659to contain probably all the JavaScript tricks known to mankind.  <a href="http://jquery.com/">jQuery</a> is
660definitely the most popular JavaScript library (to some people, it's a
661synonym to JavaScript itself).
662</p>
663<p>
664I cannot swear that there are no bugs in the generated codes, but they
665appear to work fine.
666</p>
667<p>
668Compression results:
669</p>
670<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
671<caption></caption>
672<colgroup><col align="left" /><col align="right" /><col align="right" /><col align="left" /><col align="left" />
673</colgroup>
674<thead>
675<tr><th scope="col">Library</th><th scope="col">Orig. size</th><th scope="col">UglifyJS</th><th scope="col">YUI</th><th scope="col">GCL</th></tr>
676</thead>
677<tbody>
678<tr><td>DynarchLIB</td><td>636896</td><td>241441</td><td>246452 (+5011)</td><td>240439 (-1002) (buggy)</td></tr>
679<tr><td>jQuery</td><td>163855</td><td>72006</td><td>79702  (+7696)</td><td>71858   (-148)</td></tr>
680</tbody>
681</table>
682
683
684<p>
685UglifyJS is the fastest to run.  On my laptop UglifyJS takes 1.35s for
686DynarchLIB, while YUI takes 2.7s and GCL takes 6.5s.
687</p>
688<p>
689GoogleClosure does a lot of smart ass optimizations.  I had to strive really
690hard to get close to it.  It should be possible to even beat it, but then
691again, GCL has a gazillion lines of code and runs terribly slow, so I'm not
692sure it worths spending the effort to save a few bytes.  Also, GCL doesn't
693cope with <code>eval()</code> or <code>with{}</code> &ndash; it just dumps a warning and proceeds to
694mangle names anyway; my DynarchLIB compiled with it is buggy because of
695this.
696</p>
697<p>
698UglifyJS consists of ~1100 lines of code for the tokenizer/parser, and ~1100
699lines for the compressor and code generator.  That should make it very
700maintainable and easily extensible, so I would say it has a good place in
701this field and it's bound to become the de-facto standard JS minifier.  And
702I shall rule the world. :-) Use it, and <b>spread the word</b>!
703</p>
704</div>
705
706</div>
707
708<div id="outline-container-1_6" class="outline-3">
709<h3 id="sec-1_6"><span class="section-number-3">1.6</span> Bugs? </h3>
710<div class="outline-text-3" id="text-1_6">
711
712
713<p>
714Unfortunately, for the time being there is no automated test suite.  But I
715ran the compressor manually on non-trivial code, and then I tested that the
716generated code works as expected.  A few hundred times.
717</p>
718<p>
719DynarchLIB was started in times when there was no good JS minifier.
720Therefore I was quite religious about trying to write short code manually,
721and as such DL contains a lot of syntactic hacks<sup><a class="footref" name="fnr.1" href="#fn.1">1</a></sup> such as “foo == bar ?  a
722= 10 : b = 20”, though the more readable version would clearly be to use
723“if/else”.
724</p>
725<p>
726Since the parser/compressor runs fine on DL and jQuery, I'm quite confident
727that it's solid enough for production use.  If you can identify any bugs,
728I'd love to hear about them (<a href="http://groups.google.com/group/uglifyjs">use the Google Group</a> or email me directly).
729</p>
730</div>
731
732</div>
733
734<div id="outline-container-1_7" class="outline-3">
735<h3 id="sec-1_7"><span class="section-number-3">1.7</span> Links </h3>
736<div class="outline-text-3" id="text-1_7">
737
738
739<ul>
740<li>
741Project at GitHub: <a href="http://github.com/mishoo/UglifyJS">http://github.com/mishoo/UglifyJS</a>
742</li>
743<li>
744Google Group: <a href="http://groups.google.com/group/uglifyjs">http://groups.google.com/group/uglifyjs</a>
745</li>
746<li>
747Common Lisp JS parser: <a href="http://marijn.haverbeke.nl/parse-js/">http://marijn.haverbeke.nl/parse-js/</a>
748</li>
749<li>
750JS-to-Lisp compiler: <a href="http://github.com/marijnh/js">http://github.com/marijnh/js</a>
751</li>
752<li>
753Common Lisp JS uglifier: <a href="http://github.com/mishoo/cl-uglify-js">http://github.com/mishoo/cl-uglify-js</a>
754</li>
755</ul>
756
757
758</div>
759
760</div>
761
762<div id="outline-container-1_8" class="outline-3">
763<h3 id="sec-1_8"><span class="section-number-3">1.8</span> License </h3>
764<div class="outline-text-3" id="text-1_8">
765
766
767<p>
768UglifyJS is released under the BSD license:
769</p>
770
771
772
773<pre class="example">Copyright 2010 (c) Mihai Bazon &lt;mihai.bazon@gmail.com&gt;
774Based on parse-js (http://marijn.haverbeke.nl/parse-js/).
775
776Redistribution and use in source and binary forms, with or without
777modification, are permitted provided that the following conditions
778are met:
779
780    * Redistributions of source code must retain the above
781      copyright notice, this list of conditions and the following
782      disclaimer.
783
784    * Redistributions in binary form must reproduce the above
785      copyright notice, this list of conditions and the following
786      disclaimer in the documentation and/or other materials
787      provided with the distribution.
788
789THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
790EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
791IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
792PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
793LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
794OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
795PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
796PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
797THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
798TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
799THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
800SUCH DAMAGE.
801</pre>
802
803
804
805
806</div>
807</div>
808</div>
809<div id="footnotes">
810<h2 class="footnotes">Footnotes: </h2>
811<div id="text-footnotes">
812<p class="footnote"><sup><a class="footnum" name="fn.1" href="#fnr.1">1</a></sup> I even reported a few bugs and suggested some fixes in the original
813<a href="http://marijn.haverbeke.nl/parse-js/">parse-js</a> library, and Marijn pushed fixes literally in minutes.
814</p>
815</div>
816</div>
817<div id="postamble">
818<p class="author"> Author: Mihai Bazon
819</p>
820<p class="date"> Date: 2011-04-04 00:07:13 EEST</p>
821<p class="creator">HTML generated by org-mode 7.01trans in emacs 23</p>
822</div>
823</div>
824</body>
825</html>