I will be submitting a pull request for my branch, "feature-cache". This is my first contribution to the code base of jQuery, so please be patient with me. I have tried to read all the documentation and guidelines I could find, but I may have missed something.
I would like the jQuery Team to consider adding $.cache to the jQuery API to encourage reducing the number of (repeated) DOM lookups commonly found in web applications these days -- especially with the size of JavaScript code bases increasing with larger web applications. I have written all the necessary code and placed (IMHO) where it seemed to fit best -- manipulation.js. I have successfully built jQuery and tested it. The amount of code required for this functionality leaves an extremely small footprint -- and does NOT effect other piece of code in jQuery.
Here is my implementation (reference manipulation.js):
[5320]: cache = {} // this will be used to store the user-based jquery (DOM) objects.
[5855]: cache() // this method extends jquery so that a user may create cached DOM objects (ex. future or repetitive, event-based manipulation) using the following syntax:
$.cache("aLabelForSomeDomCollection","jquerySizzleSelector"); (ex. $.cache("animImages","img.animated");)
The user can then later retrieve that (jQuery-wrapped) DOM element or collection using: $.cache("animImages"); without any internal DOM traversal... just an object reference.
$.cache("animImages").update(); is a chainable method which updates (adds/removes) based on the current state of the DOM.
Note: Since the selector property has been deprecated/removed, I'm adding it back for cached elements here (for update()).
[5865]: updateElements // this function is assigned to the update property of the cached object. This functionality may or may not prove useful in other use cases.
In summation, I've been writing JavaScript for a couple years now where I almost always cache and DOM objects that will be re-used. Not only have I seen the importance in my own code, but I've seen a growing number of web applications getting bloated with "divitus" and repeated selector lookups (even among more experienced developers I see this all too often). If jQuery had a caching mechanism introduced and promoted (especially since this code has an extremely small footprint), I believe people would use this functionality to at least speed up some web applications. I also feel that this feature fits very nicely with the fundamental principles of the JavaScript library we've all come to love!
Please provide any suggestions or thoughts. I would very much like to see if there's support for this much needed functionality in web applications, regardless of its exclusion or inclusion in jQuery.