-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcachey.js
60 lines (52 loc) · 1.72 KB
/
cachey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Cachey jQuery Plugin
*
* A collaboration between:
*
* Jeremy Kahn - [email protected]
* Ben Mills - [email protected]
*
* To use, simply load this script after you load jQuery. As elements are selected, they are cached.
* When they are called again, the cached reference is called automatically, there is no DOM traversal.
* Usage is completely transparent, just load this file and your apps will go faster. :)
*/
jQuery(function(){
setTimeout(function(){
(jQuery.fn.cachey = function() {
jQuery.noConflict();
// Cache Holder
window.cachey_cache = {},
$ = function(sel, context) {
/**
* Do not execute caching logic if:
*
* 1. sel isn't a selector.
*
* 2. A context was provided. Logic needed to account for that would slow down the plugin overall, and contexts are provided as a performance boost anyways, so caching it would yield diminishing returns.
*
* 3. sel is actually a new HTML element (The regexp was taken from the jQuery source for a similar purpose)
*/
if(typeof sel !== 'string'
|| context
|| /^[^<]*(<[\w\W]+>)[^>]*$|/.exec(sel)[1]) {
return jQuery(sel, context);
}
if (cachey_cache[sel]) {
// If selector exists, return the cached version.
return cachey_cache[sel];
} else {
// Otherwise return the selected object and add it to the cache holder.
return cachey_cache[sel] = jQuery(sel, context);
}
};
// Attach all jQuery utility functions to the new $ object
for(k in jQuery) {
$[k] = jQuery[k];
}
// Give $ (not the jQuery object) the ability flush the cache
$.flush = function(){
window.cachey_cache = {};
};
})()
}, 0);
});