191

Considering

  • business background
  • community support
  • available extensions
  • default set of features
  • simplicity of use
  • and reliability

why do you prefer one over the another?

4
  • 1
    I think it will be hard to say why one is preferred over the other until people get to experiment with applications and see what the limits of closure is. Nov 8, 2009 at 21:26
  • 3
    Some syntax comparisons here: derekslager.com/blog/posts/2010/06/…
    – user194743
    Sep 9, 2010 at 5:15
  • 4
    This article may also help with the decision. Key lesson: “Closure is an ecosystem. It is not like jQuery where you just use it here and there to make accessing or manipulating the DOM easier. If you want to truly use Closure, you have to make a commitment to truly use it.” Feb 10, 2012 at 15:56
  • 2019 What's google closure? :D
    – evilReiko
    Mar 6, 2019 at 12:19

8 Answers 8

176

I'll try to add my piece of information.

More than another JS lib

As I understand it, Google Closure is not only another JS library, but it is also a set of tools that will allow you to optimize your JS code. Working with jQuery gives you good tools and a lightweight library, but it does not minify your own code. The Closure compiler will. The closure inspector may also be useful, as sometimes minified code has a different behavior than the original one, and is a pain to debug. It integrates with Firebug and support unit tests, which are both developers' best friends nowadays.

Documentation

I guess that as any new library VS a well established one, it will lack the availability of tons of extensions and tutorial that jQuery has. However, being pushed by Google should ensure that support and reliability will be both pretty good. The current documentation and tutorial both seem really good, too.

Features

The features of Closure look decent, though, and its modular architecture is promising, too. I guess Google has been using it internally for a long time, which means that you could expect all basic features (and more) to be implemented, and probably in a very optimized and scalable way. They are trying to present it as the STL of JavaScript, so they should have polished it.

After looking at the features more closely, it seems that this may be a step forward for web-applications development compared to existing libraries as jQuery. It guess it benefits internal developments at Google, but things like detecting the online state (see goog.events.OnlineHandler), easy integration of AJAX requests and JS actions in the browser history (see goog.History), or the legions of great widgets they provide (see goog.ui package) may help all of us building even more awesome webapps ;) !

It comes with templates features that integrates with Java (who said GWT ?), so this may also be another plus for Closure.

Ease of use

Finally, it looks pretty simple to use. The syntax may be a bit more verbose than the short $ jQuery function, but with IDEs and auto-completion, it's not a real problem. Moreover, I'd say we can expect a good integration in IDEs like Eclipse, coming from Google.

EDIT: as requested, let me say a few words about the GWT reference. Google Web Toolkit is a Java library that allows to create AJAX-enabled web interfaces and that generates (and optimizes) the required JavaScript code. As Google Closure allows to create Templates that can be used both client- and server-side (using JavaScript and Java), my guess is that it will soon be possible to use them jointly (if it's not already the case).

9
  • 5
    Detailed and meaningful answer, thank you. Could you please explain what that reference to GWT means? I'm afraid I didn't get that.
    – pestaa
    Nov 7, 2009 at 19:26
  • 1
    I think you should also mention the template library (Soy) in your "More than" section. The same template file can be used in both server (Java) side and client side. Really nice implementation IMO. Means we can just send down JSON in AJAX queries rather than sending down HTML - saves bandwidth. Nov 9, 2009 at 22:34
  • 1
    I just discovered a critique against Closure, but perhaps it's just FUD: sitepoint.com/blogs/2009/11/12/…
    – nalply
    Mar 18, 2010 at 13:59
  • 7
    The critique isn't FUD, and thanks for the link. The critique is focused on how the javascript code isn't optimal everywhere. This may be valid, but it misses the larger point. As I understand it, Closure targets really complex JS applications (on the scale of GMail), and that has some consequences - such as not every line being optimal since everything isn't written by a single brilliant coder. But conversely it might scale further as a software engineering framework. Jun 1, 2010 at 5:12
  • 3
    @nalpy As suggested by the comments, this article didn;t mention that thelibrary gots a COMPILER (not a minifier) which might optimize these coding issues.
    – Ant
    Feb 21, 2012 at 8:26
30

In my brief look at the API I find the differences between jQuery and Closure to be striking.

jQuery is basically just a simplified way to do many frequent operations in a cross-browser way.

Closure is a framework that is very new, in that they provide a cross-browser way to use the <canvas> tag, for example, and they have added new events.

So, this is adding onto what we typically do with javascript, they are taking many operations that people want to do and putting them into the API.

For example, they have an event to tell if the online state has changed. So you can tell if the system is online.

They have javascript functions that use tools such as Google Gears, which continues with the fact that they have extended what can be done with Javascript.

It will take me a couple of days to digest all the changes, but I can see that this could have a big impact on web applications that can be developed.

25

The biggest advantage of Closure Library is that is is designed for Closure Compiler. This opens completely new possibilities for JavaScript development...

The compiler has several cool features:

  • It compiles readable JavaScript into compressed machine-readable JavaScript - it has the best compression ratio in "ADVANCED" mode.
  • Documentation of the code with JSDoc Tags is important: the compiler reads it and you get warnings during compilation for typos in documentation, wrong use of a @constructor, wrong type of a variable, misuse of a field annotated with @private and @protected, etc.
  • If you write a reusable JavaScript library, such as OpenLayers or Google Maps, you formally export your public API - and the compiler optimizes your internal code.
  • The end applications can be compiled together with the library - and then the unused parts of the library are removed from the produced code. Dependencies are solved automatically by the compiler.
  • Compiler accepts constants to remove unwanted functionality - this allows compilation only for particular browser such as Mobile WebKit, for only one of Quirks mode or Strict mode, compilation without support of IE6, etc.
  • Debugging with FireBug is possible even for the compiled version of the source code.
  • Compiler supports generation of dynamically loadable modules, which can significantly speed up loading of the end application, because the code for advanced functionality can be loaded only when it is required.

For details have a look at: http://blog.klokantech.com/2010/12/closure-compiler-for-openlayers-3x.html

20

Edit: take a look at this youtube video it may answer some questions about Google Closure better.

Probably the best sources of information on google closure are project discussion group, wiki, doc pages, demos and a yet unfinished book by Michael Bolin that is now available from safari books site.

one thing I can tell right away - there is a steeper learning curve for closure vs jQuery but it may be well worth it due to the library's vastness, clear organization and the benefit of using it together with the compiler and the templating tool.

closure library in that respect is more like dojo than jQuery, and some concepts were borrowed from dojo, according to Michael Bolin.

google closure compiler uses JSDoc documentation system which simultaneously (if created by the programmer correctly) provides documentation and enables catching many errors at compile time.

while function names are more verbose than jQuery's, the compiler shrinks the code (using various optimization tactics) and the type checking will save a considerable time debugging the code, so time typing in the longer names is probably not an issue. At the same time longer names add readability.

library supports browsers running in the quirks mode so that scripts could be embedded by other sites using "quirky" html

library works with (but does not depend upon) a javascript templating system called soy that simplifies filling documents with content.

like jQuery google closure allows traversing dom structure with the string-based queries using a dedicated component of the library.

closure library relies on dot-delimited namespaces more like Java - a very strong organizational feature.

using such namespaces will incur overhead in uncompiled code, but in the compiled code those things are replace with short variable names.

3
  • 1
    I was looking for something else and ended up here, but good to know like jQuery google closure allows traversing dom structure with the string-based queries using a dedicated component of the library. Can you please link me to some documentation related to it?
    – IsmailS
    Jul 29, 2011 at 7:23
  • @iSid: I believe he talks about goog.dom.DomHelper, but I personally don't see a query function equivalent to jQuery. @Evgeny: Can you give us more details?
    – exhuma
    Mar 1, 2012 at 10:42
  • @iSid: That functionality is provided by goog.dom.query, which is not documented as well as the rest of the goog.dom namespace.
    – bkirkbri
    Jul 20, 2012 at 18:27
8

I just posted a pretty exhaustive article about Google Closure which answer this question on insideRIA.

...Closure rulez! ^_^

1
  • 4
    Nice to-the-point comparison to jQuery: "[...] jQuery work really well and make possible great things with no effort and fast, so the "promise" of Closure is not to offer better algorithms but rather a really better, organized and maintainable way of deploying JavaScript applications [...] Coding in Closure means thinking with object oriented principles in mind, create several js files, each representing a single class, organize the application architecture into packages and finally deploying it using the tools provided, obtaining a single, compressed, and safe JavaScript file [...]"
    – Alex
    Jun 8, 2011 at 23:30
7

Maybe I'm not getting jQuery, but I haven't seen a real UI widgets collection there (there are plugins, yes, but you never know how well-tested they are and often there is no clear winner and/or the plugin lacks documentation).

Closure has, among other things, a widgets collection (see the demos tab), including, say, imageless buttons used in gmail.

More generally, it has more functionality implemented as part of the release. It may not be a big thing, but I get annoyed with the sea of jQuery plugins when I'm looking for something as simple as a ajax history module or autocomplete.

Overall it's a huge library + set of tools and I'll be getting acquainted with it just to know what's available.

5
  • 2
    jqueryui.com is the official ui widgets collection
    – Jourkey
    Nov 8, 2009 at 7:00
  • 1
    yeah well, judging from the site it has 6 widgets: Accordion Datepicker Dialog Progressbar Slider Tabs
    – Nickolay
    Nov 8, 2009 at 12:25
  • 3
    I've used jQueryUI and I thought it was very weak. It hardly even seems to be in development. When was the last time a new widget was added. It may be the official widgets collection, but anyone looking for a widget to use with jQuery is better off just googling for third-party jQuery widgets.
    – Nosredna
    Nov 8, 2009 at 19:18
  • 2
    jQueryUI isn't just a set of javascript widgets, it has a great css framework as well. Its been a great tool for me in styling CRUD/admin pages. Plus many of those Closure "Widgets" are 1-2 liners in jQuery, not even worth writing a plugin for.
    – Jace Rhea
    Feb 26, 2010 at 3:22
  • 2
    The appeal of JQuery is not pre-made widgets. It's making your own custom widgets in record time. I rarely touch the plug-ins, including UI. Dec 7, 2010 at 5:40
1

I appreciate most of Google's contributions to the open source community and I'm sure it's got some cool stuff but overall I find Closure bulky, overwrought, and inelegant. If you must turn everything into Java I suppose it's downright spiffy.

Edit:

Fair enough. I didn't really compare. Closure is like a giant warehouse with every possible tool you could possibly want located... somewhere. Sort of like .NET or a massive Java library. Once you find what you need, you can find highly specific stuff that does highly specific jobs. And then for production you can remove all the cruft.

JQuery on the other hand, is more of an easily modified sonic screwdriver.

2
  • 1
    I'd like to learn more about the Java version of Closure. Do you have a link for this. Thanks Dec 2, 2011 at 16:27
  • Look up 'irony tags.' You'll find the answer there. Jan 11, 2012 at 23:19
0

The Google Closure Library allows you to compile and optimize your JavaScript. It's not a library like jQuery is. jQuery is something that provides you with functions that allow you to write your own javascript faster.

Google Closure would help you make your own javascript code minimized to allow for a speedier delivery over the Internet.

Long story short, Google Closure is a tool while jQuery is a library similar to Prototype.

2
  • 6
    That is not exactly true. Google Closures also comes with its own library that is similar to jQuery and that provides functions and utilities to write your own code : code.google.com/closure/library/docs/tutorial.html
    – Wookai
    Nov 6, 2009 at 21:27
  • 2
    +1 For trying to be helpful :)
    – Andomar
    Nov 9, 2009 at 17:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.