Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to jQuery when just requiring DOM traversal, $.ajax & Deferred

Edit (2012-04-12): Since this question was asked it is now possible (as of jQuery 1.8) to make custom builds of jQuery.


For most JavaScript projects that I work on, I want a simple, light-weight UI stack.

Currently I use jQuery in my projects, however when I actually take a step back and look at the code, I'm only really using it for:

  • DOM traversal
  • jQuery.ajax
  • and Deferred

Is there another library (I don't want to handle all the various cross-browser & ES3/ES5 differences myself) that can provide me these features without all the additon stuff that I personally don't need?

Dojo springs to mind, but I have little experience with that so far, and would ideally like to hear from those who have used multiple libraries on this.

like image 438
isNaN1247 Avatar asked Apr 06 '12 15:04

isNaN1247


2 Answers

Take a look at http://microjs.com/ and use what you only need.

like image 184
Bogdan M. Avatar answered Oct 25 '22 04:10

Bogdan M.


In minimized form, Dojo is 136kb, jQuery is 96kb. Moving to dojo is not going in the right direction.

The issues you should consider are:

  1. Suitability of library for your purpose
  2. Size of library
  3. Likelihood that it will be precached already
  4. Your familiarity with the functionality of the library
  5. Availability on a popular, public CDN
  6. Good support on the net and great documentation
  7. Good reputation for reliability, cross browser support and regular updates

Go through each of these and unless you find another library that scores well on these, jQuery may be your best bet. It is surprisingly compact for what it offers you and it doesn't really have a lot of stuff that isn't in your list of things you want. jQuery has done a pretty good job of keeping the core library focused on its central mission and let UI stuff go into jQueryUI and most everything else into their own plug-in libraries.

If you're obsessed about optimizing the code you include to only be the things you need, then you may want to look at YUI. It was designed to be modular so that you can specify only the modules you want and then you can prebuild a chunk of code that only has those modules in it (or you can dynamically load just the modules you want). My sense is that YUI is somewhat overdesigned in this regard and it's cumbersome to use for quick projects because you have to spend the time to figure out which modules you need and generate that build each time. Once you get a bunch of modules loaded, it's not that compact either which is where you find that jQuery is surprisingly compact for what it includes.

In general, you should not worry about the things that a library includes that you are not using. Just look at the overall size and suitability of the libraries that do meet your needs. You can probably find a library that does only what you want and is bigger than jQuery and isn't widely cached so that wouldn't be a win.

There are compact libraries out there for just ajax or just deferred, but you probably want one with ajax and deferred implemented together so you can use deferred with ajax (like jQuery has done). Libraries that do extensive DOM manipulation tend to be more than just that because they are more designed to be your core library and most people have other needs besides just DOM manipulation.

In the end, I'd suggest that you shouldn't care what your library has in it that you don't need. Just evaluate it's overall suitability vs. the alternatives.

like image 32
jfriend00 Avatar answered Oct 25 '22 03:10

jfriend00