xhroot

Google Closure Library

Google’s Closure Library is a javascript library useful for building very large javascript applications. Combined with the Closure Compiler, it produces the smallest compressed code of all other javascript compressors, and is the engine behind Google’s javascript heavyweights: Gmail, Docs, Reader, etc.

Closure is equipped with a rich set of object/array processing (forEach, map, etc.) and DOM manipulation tools. Until recently, I had to include underscore.js along with jQuery to get the equivalent functionality.

Closure also has its own template system, a very robust XHR library which includes an XhrManager that can pool several XHR requests to save resources, a large UI library (browse the library demos to get a feel for the UI elements that are built in), the best javascript rich text editor I’ve seen to date, and several nice extras that jQuery would require plugins for, like the Url parser (goog.uri.Utils).

It’s also a very stable library. Closure is so deeply entrenched in many Google applications that they cannot make significant breaking changes at this point. It also means that new code is heavily vetted to ensure longevity. For example, to watch for events in Closure, use listen or listenOnce. How would you do it in jQuery? bind, one, click, delegate, live, on? Which of these is deprecated? How do their signatures differ? (On a side note, I really dislike jQuery’s “function overloading” which uses different types for arguments and determines intent by doing type checks).

The real boon, however, is in Closure’s ability to verify types and check for errors during compile. gjslint and fixjsstyle are useful pre-compile tools for error checking and automatically fixing formatting issues to ensure consistent readability.

Overall, I’ve been very impressed with the breadth of Closure Library. Its design and consistency across the codebase make it easy to write and maintain applications. It addresses a very particular problem in javascript extremely well - large codebases - but it cannot replace jQuery for small applications. Even still, my exposure to Closure has helped me to see how to improve my javascript in general and hopefully we’ll begin to see its ideas being incorporated into jQuery in the future.

See the API documentation to browse the library.

Comments