Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommonJS modules with Vaccine #41

Closed
wants to merge 9 commits into from
Closed

CommonJS modules with Vaccine #41

wants to merge 9 commits into from

Conversation

jacobsandlund
Copy link

I converted crossfilter to CommonJS format, and then included vaccine so it could work in the browser. 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, 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.

At first, I tried to keep the changes as small as possible (83 insertions, 56 deletions in the source files in this commit). Then, I went ahead and made a lot of changes 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:

I initally used 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

@jacobsandlund
Copy link
Author

I reduced the size even further by renaming the crossfilter/index module as crossfilter. That saved 13 bytes, to bring it down to 319 bytes (or 9.97%--hey, less than ten percent!) added overall.

The next commit removes src/version.js, and just builds it with the version parsed from package.json. This makes it so src/package.js is unnecessary, and saves another ten bytes (309, or 9.7% added). The downside being that crossfilter has no "version" property in node. If you removed "version" altogether (didn't build it in), that would save another 12 bytes (297, or 9.3% added).

@jacobsandlund
Copy link
Author

This last commit doesn't change vaccine.js functionally, it just optimizes the require routine for minification. Down to 301 bytes (9.4%) added.

@jasondavies
Copy link
Collaborator

Interesting, thanks. Since Crossfilter already works fine in both browsers and Node.js, I’m not convinced there’s any reason to change anything here.

jdar pushed a commit to jdar/crossfilter that referenced this pull request Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants