Feb 16, 2012

What's inside jQuery?

jQuery is probably one of the most widely used pieces of software all-round. When it first came out, it was sort of like a revelation to almost everyone in our field - so thank you for that, John Resig. jQuery brings the kind of convenience most of us web-developers would rather not live without. (seriously, we'd all go around killing ourselves.)

But what's inside our daily bread? Today, I'd like to take a look at precisely what's inside jQuery, in terms of bytes vs features. I will be referring to the minified size of each file - not the gzipped filesize. Your computer has to parse and run 95KB of JavaScript, regardless of how it gets delivered.

At release 1.0, jQuery was a cozy little 17KB of JavaScript. Over the years, it has grown to more than 5 times it's original size. Below is a table showing the footprint of the final release of each version.

VersionBytesWhat happened?
1.017,170minor improvements
1.0.419,274minor improvements
1.1.422,421minor improvements
1.2.655,516new AJAX features, animation/effects, some new traversal functions
1.3.256,815Sizzle was introduced
1.4.478,766substantial performance improvements
1.5.285,557new AJAX framework, deferred objects, and performance improvements
1.6.491,602various new extension-points in the API, performance improvements
1.7.197,086new event framework, performance improvements

Nobody doesn't love bar charts, so here's what that looks like:


jQuery has put on weight since 2006, no argument there. Thinking back to 2006, I have to ask: is jQuery really radically different today, compared to six years ago? Granted, it's a much better framework, a much more solid and extensible architecture, but how many of us really use it as a framework?

For most day-to-day jobs, jQuery is used for the same thing it was always used for: selection, manipulation, event management and annoying little animations. Can we really justify the 550% weight-increase?

Nutrition Facts

Let's take a look at the ingredients - what do you get in 95KB of JavaScript?

Nobody doesn't love pie, so let's start with a pretty pie chart:


This is a breakdown of the files that make up jQuery - I minified each file for comparison, so we're comparing the amount of bytes required to deliver each subset of features.

Below is a description of each of the ingredients that make up the jQuery special sauce:

FilenameBytes%Contents
sizzle.js15,57216.26%CSS3 selectors etc. - the heart of jQuery
event.js12,63213.19%event-driven programming framework and mouse/keyboard/focus events
manipulation.js9,99210.44%DOM manipulation functions
core.js9,0339.43%includes bootstrapping, browser checking, type-checking, collection helpers and JSON/XML support
ajax.js8,2058.57%cross-browser AJAX abstraction
effects.js8,0078.36%animation framework and basic slide/fade support
attributes.js7,4177.75%includes support for attributes, properties, classes
css.js4,2814.47%basic CSS/style abstraction and helpers
support.js4,1264.31%cross-browser feature availibility detection
traversing.js3,8834.06%searching and filtering operations for collections of elements
offset.js3,4333.59%cross-browser page/scroll offset checking and manipulation
data.js2,9753.11%manages metadata-association with DOM elements
queue.js1,8771.96%event queue framework (supports animation etc.)
deferred.js1,6441.72%deferred execution framework
callbacks.js1,4571.52%
dimensions.js7350.77%inner/outer width/height measurement and manipulation
sizzle-jquery.js2560.27%Sizzle.js configuration
exports.js1510.16%support for AMD proposal (RequireJS etc.)
intro.js570.06%
outro.js110.01%
Total95,744

(yum!)

A good half of that pie is the stuff that makes jQuery sizzle. Sizzle provides the CSS3 selectors you would rather not live without, and "manipulation.js" is what you need to make it edible. (the DOM, that is.) (editable, that is.)

Of course, without AJAX support and a convenient CSS abstraction, you would still want to shoot yourself. There are certainly lots of other odds and ends in there that you might someday find yourself needing. In a sense, jQuery is like a swiss army-knife for the client-side - it has a tool for everything. Even weird things like that toothpick that you'll most likely never use because you can't be entirely sure where it's been.

Yet, thinking back to 2006, I don't recall myself thinking there was really anything desperately missing in jQuery - okay, smaller things, once or twice. But it did pretty much everything I needed to do.

Of course, the medium itself has grown and evolved, and I'm not going to say that requirements and expectations haven't grown with it.

Have your needs grown by 550%?

Does jQuery do everything it used to do 5.5 times "better"?

Alternative Diet

I know, you've been drinking Coca-Cola since you were 2, and you really can't imagine there's another softdrink that can take it's place. No worries, I'll be the last person on Earth to ask you to give up Coke. Nay jQuery.

But in terms of calories (well, bytes) you may wish to supplement your diet with some slimmer alternatives.

  • There are several lightweight AJAX libraries, typically under 3KB - compare to jQuery's "ajax.js" at 8KB.
  • You can find several alternative CSS selector libraries as well, some of them under 3KB - compare to Sizzle at 15KB. Zest is one particularly interesting alternative that claims to be even faster than Sizzle, although the name isn't quite as cool.
  • Zepto is another interesting "full stack" library with many of the most essential jQuery-features in a very small package, and with a jQuery-compatible API. Compare at 20KB for the whole framework.

And last but not least, I'd like to mention a project called Ender - which makes the aggressive claim that:
"small, loosely coupled modules are the future and large, tightly-bound monolithic libraries are the past"
Granted, 95KB is not a lot in this day and age - probably not enough that anybody would go as far as to call jQuery a "monolith".

Would you?