Skip to main content

Bug Tracker

Side navigation

#13460 closed bug (plugin)

Opened February 15, 2013 08:13PM UTC

Closed February 15, 2013 08:50PM UTC

Last modified February 16, 2013 02:24AM UTC

New Feature: $.cache

Reported by: say2joe@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:
Description

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.

Attachments (0)
Change History (4)

Changed February 15, 2013 08:50PM UTC by gibson042 comment:1

resolution: → plugin
status: newclosed

First, thanks very much for your contribution. We value both your efforts and your extensive documentation. Unfortunately, this functionality is not well-suited to the core library at present. Fortunately, it is ''perfectly'' suited to adding value as a plugin. I encourage you to visit http://plugins.jquery.com/ and offer it to the community!

Changed February 15, 2013 08:54PM UTC by rwaldron comment:2

Reposted from github...

To be honest I'm not at all fond of the idea because it just means we're holding strong references to DOM nodes, which is effectively a memory leak. Instead of allowing the browser to dispose of removed nodes during GC, the nodes will remain reachable and never get disposed of; this means cache will just grow the heap and over time the browser will suffer as a result.

Changed February 15, 2013 09:04PM UTC by say2joe comment:3

This is a feature that would need to be used by to cache persistent nodes -- not nodes which may be added and removed by the application. But, GC will be considered strongly when I submit and update as a plug-in. Thanks for all the feedback!

Changed February 16, 2013 02:24AM UTC by say2joe comment:4