Last active
September 5, 2024 01:48
-
-
Save jtsternberg/1e03f5fd5be8427170c5 to your computer and use it in GitHub Desktop.
jQuery selector cache with reset (original: http://eamann.com/tech/selector-caching-jquery/). If commenting, please ping me on Twitter, same username.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Selector_Cache() { | |
var collection = {}; | |
function get_from_cache( selector, reset ) { | |
if ( undefined === collection[ selector ] || true === reset ) { | |
collection[ selector ] = jQuery( selector ); | |
} | |
return collection[ selector ]; | |
} | |
return { get: get_from_cache }; | |
} | |
var cache = new Selector_Cache(); | |
// get selector | |
cache.get( '#selector' ); | |
// get selector and reset cache | |
cache.get( '#selector', true ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this. Saved me some time. :)
It might be useful to have a way to flush the entire cache as well...