mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Add memoizeCapped.
This commit is contained in:
25
lodash.js
25
lodash.js
@@ -6220,6 +6220,26 @@
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A specialized version of `_.memoize` which clears the memoized function's
|
||||
* cache when it exceeds `MAX_MEMOIZE_SIZE`.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} func The function to have its output memoized.
|
||||
* @returns {Function} Returns the new memoized function.
|
||||
*/
|
||||
function memoizeCapped(func) {
|
||||
var result = memoize(func, function(key) {
|
||||
if (cache.size === MAX_MEMOIZE_SIZE) {
|
||||
cache.clear();
|
||||
}
|
||||
return key;
|
||||
});
|
||||
|
||||
var cache = result.cache;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the function metadata of `source` into `data`.
|
||||
*
|
||||
@@ -6486,7 +6506,7 @@
|
||||
* @param {string} string The string to convert.
|
||||
* @returns {Array} Returns the property path array.
|
||||
*/
|
||||
var stringToPath = memoize(function(string) {
|
||||
var stringToPath = memoizeCapped(function(string) {
|
||||
string = toString(string);
|
||||
|
||||
var result = [];
|
||||
@@ -10341,9 +10361,6 @@
|
||||
return cache.get(key);
|
||||
}
|
||||
var result = func.apply(this, args);
|
||||
if (cache.size === MAX_MEMOIZE_SIZE) {
|
||||
cache = cache.clear() || cache;
|
||||
}
|
||||
memoized.cache = cache.set(key, result) || cache;
|
||||
return result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user