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

2

7

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:

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.

Armlet answered 6/4, 2012 at 15:29 Comment(6)
Although it doesn't really answer the question - the upcoming 1.8 version of jQuery >should< work with closure compiler in advanced mode. This means dead code removal is a possibility if it's the "unused" portions of jQuery code that are bugging you.Gauldin
For your case jQuery is perfectly fine. And you are already used to it. And jQuery is pretty small - besides that, you can load it e.g. from the google or microsoft CDN where users will most likely have it already cached.Lananna
I think if you include jQuery from the Google CDN a large % of your visitors will already have it cached so I wouldn't bother switching. Woops too late :)Jurassic
Actually, given your requirements, it sounds like jQuery is the best choice for you.Pilsen
If you're really doing DOM traversal and not manipulation, Sizzle.js is a standalone script that implements the selector portion of jQuery. It's included in jQuery files itself, but can be used independently.Stutter
Goodness... the jQuery fanboys never stop. How does ThiefMaster know what is fine for OP's case? How does powerbuoy know what is in the visitors limited cache. There are dozens of versions (both full and minified) hosted by at least 3 major CDNs, and many more resources competing. Blind zealotry for a mediocre code library.Blocking
D
4

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.

Discordant answered 6/4, 2012 at 15:51 Comment(1)
Dojo 1.7 allows you to use only the modules you need (aka Dojo Nano, 4k zipped). You do not need to include all of Dojo "base" (the figure you quoted)Spillman
W
5

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

Whiggism answered 6/4, 2012 at 16:33 Comment(0)
D
4

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.

Discordant answered 6/4, 2012 at 15:51 Comment(1)
Dojo 1.7 allows you to use only the modules you need (aka Dojo Nano, 4k zipped). You do not need to include all of Dojo "base" (the figure you quoted)Spillman

© 2022 - 2024 — McMap. All rights reserved.