Skip to main content

A simple selector cache object that makes it easier to wrangle your references.

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');