square/crossfilter — pull request #41 by jakesandlund (2012-10-04 06:01:42+00:00)
Title: Vaccine

I converted crossfilter to CommonJS format, and then included [vaccine](https://github.com/jakesandlund/vaccine) so it could work in the browser. [Vaccine](https://github.com/jakesandlund/vaccine) is a super-simple and small-size way to get modular JavaScript. It also lets applications (or libraries) require libaries (`require('crossfilter')`).

Right now, all the info on vaccine is on the [README](https://github.com/jakesandlund/vaccine), but I will be making a blog post in the future to explain the benefits of vaccine compared to other modules-in-the-browser solutions.

I did this partly as an exercise to see the before and after size differences, but also because I will probably be working with the crossfilter code in conjunction with my work on [DataZooka](http://www.datazooka.com).

At first, I tried to keep the changes as small as possible (83 insertions, 56 deletions in the source files in [this](https://github.com/jakesandlund/crossfilter/commit/7be24b977fa0a54ca67e06f19739023d3398c880) commit). Then, I went ahead and made a lot of [changes](https://github.com/jakesandlund/crossfilter/commit/32e4867edd1ef076e156430618cb81b60fcde2e0) to how the modules are grouped. Just drop or change that commit if you don't like how it's done.

The advantages to this change are the removal of global variables, a familar module format (CommonJS), and that now the actual source files can be used in a Node.js environment. I didn't make this change, but the test files could require the individual modules, instead of relying on the crossfilter global, if desired.

The disadvantage is that this adds some size to the library.

Minified and gzipped sizes:
- [Base commit](https://github.com/square/crossfilter/commit/0a7bf20e08bc889e0ca70a3be7b5d64f45fbe596): 3201 bytes
- [Modules rearranged](https://github.com/jakesandlund/crossfilter/commit/32e4867edd1ef076e156430618cb81b60fcde2e0): 3533 bytes (added 332, or 10.4%)
- [Direct translation](https://github.com/jakesandlund/crossfilter/commit/7be24b977fa0a54ca67e06f19739023d3398c880): 3656 bytes (added 455, or 14.2%)

I initally used [vaccine_ordered.js](https://github.com/jakesandlund/vaccine/blob/master/vaccine_ordered.js). For the direct translation commit, this added 569 bytes (17.8%) from the base commit. Then I created a specially-made `vaccine.js` to reduce the size (I'm actually thinking of making it one of the default configurations). This saved 114 bytes (3.6%).

Also, note that more of the added bytes are caused from converting to CommonJS format, than from including `vaccine.js`:
- Rearranged: 112 bytes (34%) for vaccine, 220 for CommonJS
- Direct translation: 122 bytes (27%) for vaccine, 333 for CommonJS