I've actually put a lot of thought into caching - an implementation of
which would be almost exactly as you describe. There are two huge
stumbling blocks to consider though:
1) Performming any DOM modification using a non-jQuery library would
completely mess up any caching abilities. For example, if "div" was
already cached and another library added a new <div> to the page -
jQuery would have no way of figuring out that that occurred.
2) The only way that caching would be possible would be in the "pure
jQuery" environment - which I'm sure would be possible for some people
(not everyone, but a lot). Everytime a append/prepend, before/after,
html - the cache would have to be refreshed. In the end - it's really
hard to say that the benefits would be worth it.
With all that being said - I definitely want to try it "some day" -
it'll just have to wait until post-1.0 jQuery.
--John
> I had a few question regarding DOM caching and jquery and how it
> effects overall performance.
>
> Assumptions (based on my knowledge):
>
> 1) jquery does not cache its queries
> 2) In general, walking the DOM to find matching elements, classes and
> attributes does have a performance hit. I understand that their are
> speed advantages of using XPath over css selectors, etc. I will assume
> that jquery (since it is so tight) does the optimal thing when walking
> the DOM.
> 3)The user of jquery can speed up the DOM walk performance by
> providing more information [ie $('div.class') vs. $('.class') ]
>
> In the past, I have used a hash (ie the DOM cache) to store DOM
> elements after they are fetched one time. More often than not, I do
> these inital DOM walks once the DOM has been loaded (the
> $(document).ready ). From that point forward, I can use the hash to
> retrieve the actual DOM objects and pass them to jquery. This avoids
> DOM walks on every jquery usage.
>
> Questions:
>
> 1) Do browsers implement a DOM cache for you?
> 2) Has any thought been given to a plugin or a modification to jquery
> to cache the DOM objects resulting from queries for laster use?
> An high level implementation would be:
> - The hash key could be the query (ie "div.class" or whatever jquery
> expression you pass to $())
> - The value would be the DOM object.
> When a query is made, the cache would be checked first, if it is
> there, it is used. If it is not there, it could be retrieved (DOM
> walk) and then also stored in the cache.
> - You would have to allow for an overide of the cache in case you
> added to the DOM since last query.
>
> Please let me know your thoughts and feedback. In addition, don't be
> afraid to tell me if I am completely off base!!
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/