-
-
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> |
Yikes, we also need to account calls to the jQuery object directly (supplying no parameters).
Can do this:
for(k in jQuery) {
$[k] = jQuery[k];
}
Updated!
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]'
You're right. Not sure how to solve that, since context is often an object, or even "this."
Also, we need to account for cases the "selector" is an Object, not necessarily a function. Hm.
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
This a collaboration with http://github.com/benmills and http://github.com/draggor.
Still in progress.