Thoughts on development, design and the world we live in.

Manipulating the DOM, Google Closure vs. jQuery

By Kane Baccigalupi in JavaScript. Posted on March 9th

Comparing closure to jQuery is like comparing an apple to a fruit basket.

Google Closure, a JavaScript library and compiler, features cross-browser event management, drag and drop support, shared server and client side templating, and a massively efficient code compressor. Closure was used and refined at Google for years before it was released at the end of 2009 as open-source.

jQuery is a tool for making cross-browser dom manipulation, event management, and Ajax interactions easy and fast. It was built with the philosophy that the API should be concise and intuitive.

We can use jQuery and Closure together; however, using Closure’s own libraries for DOM manipulation, we should get smaller, faster apps.

To understand Closure’s DOM library, the rest of this article looks at:

DOM Search

The easiest way to detect the differences between the way these two libraries find elements is to look at a nested search.

jQuery’s API uses arbitrarily complex css selectors to find elements. When the elements are returned, they are wrapped with jQuery’s DOM manipulation functionality.

Closure on the other hand prefers using a number of methods for making very particular kinds of selections. The returned value is a DOM node, which can then be passed to other functions. A similar element query in Closure can be seen below:

goog.dom.query to the Search Rescue

Clearly some Closure developers felt the pain of DOM navigation. A library, ‘goog.dom.query’, was built by third-party engineers, to emulated the DOM searching capabilities of Dojo, http://dojotoolkit.org/. The above closure code can be reduced to:

Documentation for this library is more scant than in other places in Closure. There does not appear to be a way to pass in a DOM node to act as the root element for the search.

Element Creation

The abilities of jQuery and Closure are more closely aligned when it comes to the creation of DOM elements.

jQuery leaves the developer to build the base html tag for an element out of strings. After, creating the element, it is easy to add attributes to the tags. jQuery also makes it easy to append nodes to elements.

Closure lets its developers create elements on a lower level, by just passing in the tag name. The second argument is a hash of properties. Alternately, the second argument can be a string, which will be understood as the class name. Finally, the third argument defines the inner content of the element. The inner content can be text or it can be another created element.

While simple string concatenation is intuitive in jQuery,  the Closure approach will yield a more robust implementation of an HTML tag. Its nested creation syntax, though cumberson to read at first, will likely be more powerful to use.

Element Manipulation

The last area that I wanted to address in the comparison between jQuery and Closure is the manipulation of individual DOM element attributes. The two main attributes of concern are classes and styles, which require adding and subtracting values from a string, not simply replacing the value.

Adding and Removing Classes

jQuery and Closure have fairly similar interfaces for changing class names on an element:

This is an area where I think the Closure syntax is very intuitive. The namespacing with small function names is very easy to read and understand. Within this library there are also intuitive methods like ‘has’, which returns a boolean value indicating whether the class name is in the string.

Styling Elements

Closure organizes its ‘goog.style’ namespace into a series of getters and setters for individual style attributes. jQuery uses a single methods, with two signatures. One signature is for getting, the other is for setting.

Closure provides a number of very specific setters and getters in addition to these two basic ones. The more specific setters sometimes take exotic arguments designed to ease the pain of CSS calculations. For example, the ‘setContentBoxSize’ method takes as its second argument a ‘goog.math.Size’ object in order to set the dimensions of the element.


Both Closure and jQuery get the job of DOM navigation done using different philosophies and styles.

Part of Closure’s steeper learning curve is not only its sparse documentation, but that every namespace uses different conventions for API design. The diversity of conventions means that we need to do more memorization of API method names, argument signatures and return types. With jQuery there is single pattern that is applied consistently.

The consistency of jQuery’s API is a big plus for me. I find that when I am not thinking about the minutiae of my tools, I am better able to keep my focus on solving the problems at hand. That translates to faster development and faster time to market.


  1. Posted December 11, 2012 at 5:33 am | Permalink

    Nice one would love to see more comparsions :)

  2. frob
    Posted May 12, 2013 at 10:23 pm | Permalink

    Closure may look clunky but you didn’t mention one killer feature: it has a huge set of controls. jQuery UI has only a very small set of controls - nothing close to what’s needed for a commercial application. If you need a text editor, editable table, combo box, etc. etc. you’ll be out of luck with jQuery, trying to get lousy 3rd-party plugins to work, or spending thousands of dollars on professional controls library.

    With closure the controls are built in. Eat that, jQuery.

Post a Comment

Your email is never shared. Required fields are marked *