I recently wrote a small script for caching results of jQuery calls, which I've titled jQuery DOM Cache. It works just like jQuery proper in that you pass it a selector and it returns a collection of matching DOM elements. The difference is that it stores the result of each selector you pass it for recall later.
For example, you could do this:
$.domCache('#foo').show();
…
$.domCache('#foo').hide();
In this case, it would only query the DOM once. But this example isn't that interesting - you can already do that by storing the element in a variable:
var foo = $('#foo');
foo.show();
…
foo.hide();
That works. But what if the show line and the hide line were in different scripts on the same page? Or different scopes? jQuery DOM Cache doesn't care - since it's attached to the jQuery namespace, it works everywhere. In the first example, the show line and the hide line could be in completely different scripts or scopes and it would still only query the DOM once.
Modern web applications do not feature a static DOM. If you're building an application of any complexity, you'll be adding and removing elements all the time.
When you cache the results of a selector in a variable and the matching elements eventually disappear from the DOM, references to them still exist in the cached jQuery collection, so you could (if you wanted) manipulate or re-inject them into the live DOM. Same goes for jQuery DOM Cache.
You may, however, want an element to be gone for good and not remain in the cache. You can do this by passing the selector and Boolean false:
$.domCache('#foo'); // Add #foo to the cache.
$.domCache('#foo').remove(); // Delete #foo from the DOM.
$.domCache('#foo', false); // Delete #foo from the cache
Additionally, you may have a selector cached that may now match a new set of elements, but new elements will not be reflected in the cache. You can also pass Boolean true as a second argument to force the cache to refresh:
$.domCache('#foo'); // Add #foo to the cache.
$.domCache('#foo', true); // Update the cache by forcing a re-query.
Objects can be cached too. For example, it's often useful to pass DOM objects - this, window, document, etc - into jQuery. DOM Cache handles these, too, but with a bit more syntax:
$.domCache('doc', document);
$.domCache('doc'); // Returns the same as $(document)
Also, existing jQuery objects can be cached and it won't pass through jQuery again internally:
$.domCache('bar', $('#baz'));
This would be pretty pointless if it weren't faster than querying the DOM all the time. I did a few tests and calling $.domCache('#test') multiple times tends to be about 3x faster than calling $('#test') the same number of times. It's not a massive gain - on the order of microseconds, probably - but taken across an entire web application could translate into saving some milliseconds here and there.
jQuery DOM Cache isn't for everything. If you've got a small site with a few scripts, you probably won't see any benefit. But if you're working on an large-scale website or web application - with multiple loosely-coupled scripts, modules, singletons, classes, whatever-you-want-to-call-its - it might just save you a few milliseconds.
Check out jQuery DOM Cache on GitHub