Kcko
3/23/2018 - 3:14 PM

Selector Caching in jQuery

function SelectorCache() {
    var collection = {};
    function getFromCache(selector) {
        if (undefined === collection[selector]) {
            collection[selector] = $(selector);
        }
        return collection[selector];
    }
    return {
        get: getFromCache
    };
}
 
var selectors = new SelectorCache();
 
// Usage $('#element') becomes:
selectors.get('#element');