

Introducing Closure Tools - txxxxd
http://googlecode.blogspot.com/2009/11/introducing-closure-tools.html

======
jbyers
For one of our JS bundles it's a substantial improvement over YUI's
Compressor.

Original: 459220

YUI: 241920

Google: 209929 -- 13% improvement

YUI + gz: 66164

Google + gz: 61297 -- 7% improvement

Caught a few non-fatal syntax errors too. We serve hundreds of thousands of
those a day, so that's already committed to our tree and on the way to
testing.

~~~
lpolovets
Here's another data point:

I downloaded jquery-1.3.2.js and jquery-1.3.2.min.js from
<http://docs.jquery.com/Downloading_jQuery#Current_Release>

> wc -c jquery-1.3.2.js

120763

> wc -c jquery-1.3.2.min.js

57254

> java -jar compiler.jar --js jquery-1.3.2.js | wc -c

55340

Using Google's compiler saves an additional 4% over the existing minified
version

(disclosure: I used to work for Google)

~~~
jeresig
I definitely plan on switching jQuery over to using this compressor - excited
by the output. However it appears as if it crashes when you push the latest
nightlies of jQuery in. Not sure what's up with that.

~~~
jauderho
Out of curiousity, I threw together a quick comparison using SlickSpeed and
the latest jQuery/Prototype frameworks.

<http://bit.ly/1Ei4o8>

If someone wants to contribute a jQuery version compiled with advanced opts,
I'll be happy to add it to the list.

------
phren0logy
I'm pretty sure this is what Steve Yegge was talking about on the most recent
StackOverflow podcast he was in.

If that is accurate, it's a JavaScript compiler that supports a way of
partially compiling to allow very tight integration with an IDE (or editor).
Yegge's point was that an IDE or editor should not have to re-invent the wheel
and a compiler should be able to provide sophisticated feedback about syntax,
objects, variables, scope, etc.

Cool to see it out in the wild.

~~~
lupin_sansei
Yes here's the link <http://blog.stackoverflow.com/2009/04/podcast-50/>

------
gruseom
Would anyone who already knows this tool, or is taking the time to learn it,
care to explain what they mean by "compiles web apps down into compact, high-
performance JavaScript"? I assume this is a JS-to-JS translator. How exactly
are they transforming the JS?

I mean, of course, besides the obvious things they mention: minification,
removing dead code, and some error checking.

~~~
seiji
Take a look here: [http://code.google.com/closure/compiler/docs/api-
tutorial3.h...](http://code.google.com/closure/compiler/docs/api-
tutorial3.html#better)

When you turn on advanced mode they do automatic inlining, variable
substitution, and they don't respect the globalness of anything unless you
specifically force something to be global.

YUI Compressor assumes anything not in a function is global, so it won't
rename top level objects. Closure in advanced mode will trample through your
globals and property names like rhinos mating in a field of daises.

~~~
lupin_sansei
I get concerned that these minimisers and js to js compilers will introduce
subtle bugs and changes into the behaviour of my code. Is that concern
warranted?

~~~
seiji
Closure advanced mode will absolutely screw with your code.

Closure simple mode and YUI Compressor won't change the behavior of your code.
They just rename completely encapsulated objects with shorter names and remove
unnecessary semicolons.

~~~
gruseom
If the Closure "compiler" changes the behavior of a program, that means it's
generating incorrect code. Can you give an example?

~~~
seiji
Check out [http://code.google.com/closure/compiler/docs/api-
tutorial3.h...](http://code.google.com/closure/compiler/docs/api-
tutorial3.html#propnames)

(short version: inconsistencies between Closure renaming my_object.attribute
to a minified a.a while in other places you use my_object['attribute'] which
it can't minify to be a.a. You end up with a.a = 3 and a['attribute'] =
{something else}).

~~~
gruseom
Ah. This is a classic problem with transforming JS. We've run into something
similar with Parenscript (but were able to correct it, rather than say "just
don't write code that does X").

------
slig
It took me a while to figure this out, but there's a lot more than "just" a js
compiler. Since nobody here commented about it, I guess I'm not the only one
that didn't see it.

We can now use all the google fancy widgets (from gmail, gdocs, etc). Check
this: [http://closure-
library.googlecode.com/svn/trunk/closure/goog...](http://closure-
library.googlecode.com/svn/trunk/closure/goog/docs/index.html)

------
rbarr
What an unfortunate name. Perhaps, in the confusion, it will help to actually
promote Rich Hickey's wonderful work.

~~~
frognibble
It was called Closure before Rich announced Clojure to the world.

~~~
flatline
"Closure" being a general concept in programming, I always disliked the name
"Clojure". Same thing as Google Chrome, everyone is now confused when you talk
about browser chrome as a general thing.

~~~
jriddycuz
I agree that "Clojure" is kinda a silly name, but what else are you gonna call
it? I assume Henly wanted a name that conveyed its functional and/or Lisp
roots, and the fact that it's on the JVM.

Let's try some names using the usual JVM language conventions: _JLisp_ works,
but it's boring, and _Jisp_ sounds kinda suspect. _Parenjases_ is just silly.
Or maybe _LLBeans_ for "lazy lisp beans"...but that's just getting too cute.

I think I probably would have gone with _Jambda_.

~~~
adriand
"what else are you gonna call it?"

How about "Armadillo"?

~~~
oliverkofoed
Years ago, i wrote a compressor for javascript.

I called it "Arnold" because it was strong and squeezed javascript into a
smaller shape.

------
xal
I think there is way to much attention on the compiler. It's neat but not that
big of a deal. The key here is that they released their widget library. Tons
of great stuff with very high code quality. This also includes the document
editor from google docs which is by leaps and bounds the best engineered
WYSIWYG editor out there.

------
Elfan
Using this code on the code here (except for processing.js):
[http://github.com/cburroughs/minification-
compare/tree/maste...](http://github.com/cburroughs/minification-
compare/tree/master/sample-files/js/)

Latest full release of yuicompressor vs closure at SIMPLE_OPTIMIZATIONS level:

    
    
      ----- TOTALS -----
      base:  yui-compressor-2.4.2
      ('abs diff:', '768050', 'gz: ', '184071')
      ('% change: ', -0.56976157623774126, 'gz: ',   -0.51009685330672982)
      challenger: closure-compiler
      ('abs diff:', '814830', 'gz: ', '193817')
      ('% change: ', -0.60446432545511197, 'gz: ', -0.53710493134361448)

------
leif
Wow, I have been seriously missing this outside google-land. So happy it's
released; it's a fantastic set of tools, even if you only use the compiler as
a glorified lint.

~~~
fortes
Given your experience with the tool, any tips or best practices?

I already use lint in my build system, this looks like a good companion as
well.

~~~
leif
IIRC, the framework is largely based on dojo, so if you're familiar with that
you'll have an easier time getting used to it.

Don't remember many specifics, I just remember liking it. Good luck!

------
justinhj
So now there is Clozure common lisp, Clojure for the JVM, and now Closure.

I was already getting the first two mixed up.

~~~
DannoHung
I am never going to be able to refer to an actual closure again and have
anyone understand what I'm talking about, am I?

~~~
harto
I already find that hard enough - I've found that people tend to conflate
first-class functions with closures.

~~~
mjs
Well, is there any real different between a first-class function and a closure
that closes over nothing?

Is it possible for a language to support closures without first-class
functions? How common is it to have first-class functions without closures? (I
think Python sort of had this arrangement, but I think this is fixed now.)

~~~
andreyf
A closure is just a (function, bindings) pair, so Python certainly has
closures. The problem with Python is that there is no syntax for assigning to
variables declared in an arbitrary scope, because there are no explicit
declarations. As far as I understand, implicit local variables are what causes
the problems in Python, not anything about closures, really. This causes other
problems, too, such as those PG described here [1], under the heading
"Implicit local variables conflict with macros".

1\. <http://www.paulgraham.com/arclessons.html>

~~~
micheles
Python 3 has read-write closures. Check out the nonlocal declaration.

~~~
andreyf
_The nonlocal statement causes the listed identifiers to refer to previously
bound variables in the nearest enclosing scope._ [1]

While certainly a big improvement, we still can't access variables in
_arbitrary_ scopes, just local, nearest non-local, and global. To me, this
seems a significantly more complex way of specifying variable scope than just
declaring it explicitly.

1\.
[http://docs.python.org/dev/3.0/reference/simple_stmts.html#t...](http://docs.python.org/dev/3.0/reference/simple_stmts.html#the-
nonlocal-statement)

------
niels
I've been curious about some of the tools behind Google's javascript power.
The Closure Library coupled with the compiler looks really useful for creating
complex and fast web apps.

------
asher
This is wonderful news; I'm really grateful to Google for opening this.

We already knew that Google's major javascript apps were not using GWT. I
always had reservations about GWT because I think javascript is a very nice
language. (Just the language - not necessarily the DOM or implementations).

While java seems like a step backwards.

The main thing missing from javascript is some stricter checking, and it looks
like Closure Tools provides this.

------
leej
i DONT get this attitude of Google. even MS uses jquery but the open source
king Google do not. is this so hard to improve upon Firefox or Ubuntu or even
much less complex jquery?

~~~
jonknee
Google uses (and hosts free for everyone) jQuery. You can even use their new
compiler to get a smaller version of jQuery. The widget library has some
overlap with jQuery, but does so much more and has been in progress since well
before jQuery UI (Google bought Writely in March 2006 and jQuery UI didn't
come out until September 2007).

~~~
leej
MS uses and hosts free too. what is your point? i'm talking about progressing
current successful open source projects. fyi, its library has overlap with
jquery core too!

