Skip to content

Instantly share code, notes, and snippets.

@jeremyckahn
Forked from draggor/CacheyCache.html
Created September 17, 2010 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyckahn/584650 to your computer and use it in GitHub Desktop.
Save jeremyckahn/584650 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="arcus">
<!-- Date: 2010-08-04 -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$.noConflict();
// Cache Holder
var cachey_cache = {},
$ = function(sel, context) {
if (!sel){
return jQuery;
}
if(sel instanceof Function || /^[^<]*(<[\w\W]+>)[^>]*$|/.exec(sel)[1]) {
return jQuery(sel, context);
}
var selAndContext = sel + (context || '');
if (cachey_cache[selAndContext]) {
// If selector exists, return the cached version.
return cachey_cache[selAndContext];
} else {
// Otherwise return the selected object and add it to the cache holder.
return cachey_cache[selAndContext] = jQuery(sel, context);
}
};
// Attach everything in the jQuery.fn namespace
for(k in jQuery) {
$[k] = jQuery[k];
}
// Testing Code
$(function() {
var lisToMake = 5000;
console.log($("#test").html());
console.log($("#test").html()); // This selection will be cached
$('<button>')
.html('Select with jQuery()')
.click(function(){
var start = new Date();
for (var i = 0; i < lisToMake; i++){
jQuery('.li' + i).css({background : 'red'});
}
alert(new Date() - start)
})
.appendTo('body');
$('<button>')
.html('Select with $()')
.click(function(){
var start = new Date();
for (var i = 0; i < lisToMake; i++){
$('.li' + i).css({background : 'lime'});
}
alert(new Date() - start)
})
.appendTo('body');
$('<button>', { value : 'arguments', name : 'work'})
.click(function(){
var ul, li, i;
ul = $('<ul>').appendTo('body');
for (i = 0; i < lisToMake; i++){
$('<li>', { class : 'li' + i})
.html(i.toString())
.appendTo('body ul')
}
})
.html('Make a big list!').appendTo('body')
});
</script>
</head>
<body>
<div id="test">hello world</div>
</body>
</html>
@jeremyckahn
Copy link
Author

This a collaboration with http://github.com/benmills and http://github.com/draggor.

Still in progress.

@jeremyckahn
Copy link
Author

Yikes, we also need to account calls to the jQuery object directly (supplying no parameters).

@draggor
Copy link

draggor commented Sep 18, 2010

Can do this:

for(k in jQuery) {
$[k] = jQuery[k];
}

@jeremyckahn
Copy link
Author

Updated!

@draggor
Copy link

draggor commented Sep 18, 2010

Fairly certain the trick you're doing with caching and the context doesn't work. With arrays it will stringify somewhat, however maps seem to go to '[object object]'

@jeremyckahn
Copy link
Author

You're right. Not sure how to solve that, since context is often an object, or even "this."

@jeremyckahn
Copy link
Author

Also, we need to account for cases the "selector" is an Object, not necessarily a function. Hm.

@draggor
Copy link

draggor commented Sep 18, 2010

If it's an object then it should have a type which we can do an instanceof or typeof() call.

As for the context being an object problem, I think if you're passing that in, maybe you don't cache? Another option is giving objects unique IDs and storing those, using something like http://stackoverflow.com/questions/2020670/javascript-object-id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment