mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Add _.memoize cache limit.
This commit is contained in:
26
lodash.js
26
lodash.js
@@ -23,6 +23,9 @@
|
|||||||
/** Used to stand-in for `undefined` hash values. */
|
/** Used to stand-in for `undefined` hash values. */
|
||||||
var HASH_UNDEFINED = '__lodash_hash_undefined__';
|
var HASH_UNDEFINED = '__lodash_hash_undefined__';
|
||||||
|
|
||||||
|
/** Used as the maximum memoize cache size. */
|
||||||
|
var MAX_MEMOIZE_SIZE = 500;
|
||||||
|
|
||||||
/** Used as the internal argument placeholder. */
|
/** Used as the internal argument placeholder. */
|
||||||
var PLACEHOLDER = '__lodash_placeholder__';
|
var PLACEHOLDER = '__lodash_placeholder__';
|
||||||
|
|
||||||
@@ -1897,6 +1900,7 @@
|
|||||||
* @memberOf Hash
|
* @memberOf Hash
|
||||||
*/
|
*/
|
||||||
function hashClear() {
|
function hashClear() {
|
||||||
|
this.size = 0;
|
||||||
this.__data__ = nativeCreate ? nativeCreate(null) : {};
|
this.__data__ = nativeCreate ? nativeCreate(null) : {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1911,7 +1915,9 @@
|
|||||||
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
||||||
*/
|
*/
|
||||||
function hashDelete(key) {
|
function hashDelete(key) {
|
||||||
return this.has(key) && delete this.__data__[key];
|
var result = this.has(key) && delete this.__data__[key];
|
||||||
|
this.size -= result ? 1 : 0;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1958,6 +1964,7 @@
|
|||||||
*/
|
*/
|
||||||
function hashSet(key, value) {
|
function hashSet(key, value) {
|
||||||
var data = this.__data__;
|
var data = this.__data__;
|
||||||
|
this.size += this.has(key) ? 0 : 1;
|
||||||
data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
|
data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -1997,6 +2004,7 @@
|
|||||||
* @memberOf ListCache
|
* @memberOf ListCache
|
||||||
*/
|
*/
|
||||||
function listCacheClear() {
|
function listCacheClear() {
|
||||||
|
this.size = 0;
|
||||||
this.__data__ = [];
|
this.__data__ = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2022,6 +2030,7 @@
|
|||||||
} else {
|
} else {
|
||||||
splice.call(data, index, 1);
|
splice.call(data, index, 1);
|
||||||
}
|
}
|
||||||
|
--this.size;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2069,6 +2078,7 @@
|
|||||||
index = assocIndexOf(data, key);
|
index = assocIndexOf(data, key);
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
++this.size;
|
||||||
data.push([key, value]);
|
data.push([key, value]);
|
||||||
} else {
|
} else {
|
||||||
data[index][1] = value;
|
data[index][1] = value;
|
||||||
@@ -2111,6 +2121,7 @@
|
|||||||
* @memberOf MapCache
|
* @memberOf MapCache
|
||||||
*/
|
*/
|
||||||
function mapCacheClear() {
|
function mapCacheClear() {
|
||||||
|
this.size = 0;
|
||||||
this.__data__ = {
|
this.__data__ = {
|
||||||
'hash': new Hash,
|
'hash': new Hash,
|
||||||
'map': new (Map || ListCache),
|
'map': new (Map || ListCache),
|
||||||
@@ -2128,7 +2139,9 @@
|
|||||||
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
* @returns {boolean} Returns `true` if the entry was removed, else `false`.
|
||||||
*/
|
*/
|
||||||
function mapCacheDelete(key) {
|
function mapCacheDelete(key) {
|
||||||
return getMapData(this, key)['delete'](key);
|
var result = getMapData(this, key)['delete'](key);
|
||||||
|
this.size -= result ? 1 : 0;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2168,7 +2181,11 @@
|
|||||||
* @returns {Object} Returns the map cache instance.
|
* @returns {Object} Returns the map cache instance.
|
||||||
*/
|
*/
|
||||||
function mapCacheSet(key, value) {
|
function mapCacheSet(key, value) {
|
||||||
getMapData(this, key).set(key, value);
|
var data = getMapData(this, key),
|
||||||
|
size = data.size;
|
||||||
|
|
||||||
|
data.set(key, value);
|
||||||
|
this.size += data.size == size ? 0 : 1;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10316,6 +10333,9 @@
|
|||||||
return cache.get(key);
|
return cache.get(key);
|
||||||
}
|
}
|
||||||
var result = func.apply(this, args);
|
var result = func.apply(this, args);
|
||||||
|
if (cache.clear && cache.size === MAX_MEMOIZE_SIZE) {
|
||||||
|
cache = cache.clear() || cache;
|
||||||
|
}
|
||||||
memoized.cache = cache.set(key, result);
|
memoized.cache = cache.set(key, result);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1309,7 +1309,7 @@
|
|||||||
var args,
|
var args,
|
||||||
iteration = 0,
|
iteration = 0,
|
||||||
objects = [{ 'a': 1 }, { 'a': 2 }],
|
objects = [{ 'a': 1 }, { 'a': 2 }],
|
||||||
stack = { '__data__': { '__data__': [objects, objects.slice().reverse()] } },
|
stack = { '__data__': { '__data__': [objects, objects.slice().reverse()], 'size': 0 } },
|
||||||
expected = [1, 2, 'a', objects[0], objects[1], stack];
|
expected = [1, 2, 'a', objects[0], objects[1], stack];
|
||||||
|
|
||||||
fp.isEqualWith(function() {
|
fp.isEqualWith(function() {
|
||||||
@@ -1335,7 +1335,7 @@
|
|||||||
|
|
||||||
var args,
|
var args,
|
||||||
objects = [{ 'a': 1 }, { 'a': 2 }],
|
objects = [{ 'a': 1 }, { 'a': 2 }],
|
||||||
stack = { '__data__': { '__data__': [] } },
|
stack = { '__data__': { '__data__': [], 'size': 0 } },
|
||||||
expected = [2, 1, 'a', objects[1], objects[0], stack];
|
expected = [2, 1, 'a', objects[1], objects[0], stack];
|
||||||
|
|
||||||
fp.isMatchWith(function() {
|
fp.isMatchWith(function() {
|
||||||
@@ -1444,7 +1444,7 @@
|
|||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var args,
|
var args,
|
||||||
stack = { '__data__': { '__data__': [] } },
|
stack = { '__data__': { '__data__': [], 'size': 0 } },
|
||||||
expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack];
|
expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack];
|
||||||
|
|
||||||
fp.mergeWith(function() {
|
fp.mergeWith(function() {
|
||||||
@@ -1477,7 +1477,7 @@
|
|||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var args,
|
var args,
|
||||||
stack = { '__data__': { '__data__': [] } },
|
stack = { '__data__': { '__data__': [], 'size': 0 } },
|
||||||
expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack];
|
expected = [[1, 2], [3], 'a', { 'a': [1, 2] }, { 'a': [3] }, stack];
|
||||||
|
|
||||||
fp.mergeAllWith(function() {
|
fp.mergeAllWith(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user