diff options
Diffstat (limited to 'src/selector/createCache.js')
-rw-r--r-- | src/selector/createCache.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/selector/createCache.js b/src/selector/createCache.js new file mode 100644 index 000000000..18e255d0f --- /dev/null +++ b/src/selector/createCache.js @@ -0,0 +1,26 @@ +import jQuery from "../core.js"; + +/** + * Create key-value caches of limited size + * @returns {function(string, object)} Returns the Object data after storing it on itself with + * property name the (space-suffixed) string and (if the cache is larger than Expr.cacheLength) + * deleting the oldest entry + */ +function createCache() { + var keys = []; + + function cache( key, value ) { + + // Use (key + " ") to avoid collision with native prototype properties + // (see https://github.com/jquery/sizzle/issues/157) + if ( keys.push( key + " " ) > jQuery.expr.cacheLength ) { + + // Only keep the most recent entries + delete cache[ keys.shift() ]; + } + return ( cache[ key + " " ] = value ); + } + return cache; +} + +export default createCache; |