At Monetate, we write our front-end scripts mostly in raw Javascript, but we do use the Google Closure Library for some common tasks.

If you’re not familiar with the Closure Library, it’s a bit different from jQuery and most other popular Javascript libraries. With those libraries, you link to a single external script and get access to a fairly robust core group of functions (with the ability to add various extensions).

The Closure Library, on the other hand, doesn’t really have a core; all of its functions are found within discrete modules that must be specifically included in a script. (In that respect, it’s kind of like the C to jQuery’s PHP.)

The Closure Library is intended to be used in combination with the Google Closure Compiler, a command-line tool that performs sophisticated Javascript optimization. Consequently, the compiled version of your script only includes the library components that you actually need. In contrast, if you want to use just a single jQuery function on your page, you’ve got to deal with the overhead of a bunch of other unused functions that are included in the core.

Code comparisons

For those of you who are curious about how the syntax of Google Closure Library compares with jQuery, here are some side-by-side comparisons. (They’re technically top-by-bottom comparisons, but that doesn’t have the same ring to it.)

General selector function

Google Closure Library:

jQuery:

Iterating over DOM items

Google Closure Library:

jQuery:

Iterating over an array

Google Closure Library:

jQuery:

Array map

Google Closure Library:

jQuery:

Insert element into DOM, relative to existing element

Google Closure Library:

jQuery:

Find closest matching ancestor by class

Google Closure Library:

jQuery:

JSON string parsing

Google Closure Library:

jQuery:

Add and remove classes

Google Closure Library:

jQuery:

As you can no doubt tell, the jQuery code is a bit more concise, at least compared with the Google Closure Library’s source — but once that source is compiled, you see the gains.

Implementation

If you just want to get something up and running quickly, jQuery is the simpler choice: Just link to a single external Javascript file. (You don’t even have to host it yourself; just link to a version hosted by Google’s content distribution network.)

With the Closure Library, you download the entire library, then include a reference to the core library file, base.js, in your HTML, as well as references to required modules in your JS. If you intend to use the library in combination with the Closure Compiler, you’ll also have to download additional files and then compile your scripts before they’re used.

At Monetate, our high-volume production needs make the trade-off well worth it. Using the Closure Library and the Closure Compiler, we can generate much more spartan code than would be possible with a conventional Javascript library, and because both tools were developed by Google for its own enterprise needs, we can be confident that they are well-tested and designed with scalability in mind.