mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Use Object.create(null) in SetCache.
This commit is contained in:
committed by
jdalton
parent
f039d5bbcb
commit
70617be903
@@ -756,6 +756,7 @@
|
|||||||
|
|
||||||
/* Native method references for those with the same name as other `lodash` methods. */
|
/* Native method references for those with the same name as other `lodash` methods. */
|
||||||
var nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
|
var nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
|
||||||
|
nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
|
||||||
nativeIsFinite = context.isFinite,
|
nativeIsFinite = context.isFinite,
|
||||||
nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
|
nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
|
||||||
nativeMax = Math.max,
|
nativeMax = Math.max,
|
||||||
@@ -1348,7 +1349,7 @@
|
|||||||
function SetCache(values) {
|
function SetCache(values) {
|
||||||
var length = values ? values.length : 0;
|
var length = values ? values.length : 0;
|
||||||
|
|
||||||
this.data = { 'number': {}, 'set': new Set };
|
this.data = { 'hash': nativeCreate(null), 'set': new Set };
|
||||||
while (length--) {
|
while (length--) {
|
||||||
this.push(values[length]);
|
this.push(values[length]);
|
||||||
}
|
}
|
||||||
@@ -1364,9 +1365,8 @@
|
|||||||
* @returns {number} Returns `0` if `value` is found, else `-1`.
|
* @returns {number} Returns `0` if `value` is found, else `-1`.
|
||||||
*/
|
*/
|
||||||
function cacheIndexOf(cache, value) {
|
function cacheIndexOf(cache, value) {
|
||||||
var type = typeof value,
|
var data = cache.data,
|
||||||
data = cache.data,
|
result = (typeof value == 'string' || isObject(value)) ? data.set.has(value) : data.hash[value];
|
||||||
result = type == 'number' ? data[type][value] : data.set.has(value);
|
|
||||||
|
|
||||||
return result ? 0 : -1;
|
return result ? 0 : -1;
|
||||||
}
|
}
|
||||||
@@ -1380,13 +1380,11 @@
|
|||||||
* @param {*} value The value to cache.
|
* @param {*} value The value to cache.
|
||||||
*/
|
*/
|
||||||
function cachePush(value) {
|
function cachePush(value) {
|
||||||
var data = this.data,
|
var data = this.data;
|
||||||
type = typeof value;
|
if (typeof value == 'string' || isObject(value)) {
|
||||||
|
|
||||||
if (type == 'number') {
|
|
||||||
data[type][value] = true;
|
|
||||||
} else {
|
|
||||||
data.set.add(value);
|
data.set.add(value);
|
||||||
|
} else {
|
||||||
|
data.hash[value] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3070,7 +3068,7 @@
|
|||||||
* @param {Array} [values] The values to cache.
|
* @param {Array} [values] The values to cache.
|
||||||
* @returns {null|Object} Returns the new cache object if `Set` is supported, else `null`.
|
* @returns {null|Object} Returns the new cache object if `Set` is supported, else `null`.
|
||||||
*/
|
*/
|
||||||
var createCache = !Set ? constant(null) : function(values) {
|
var createCache = !(nativeCreate && Set) ? constant(null) : function(values) {
|
||||||
return new SetCache(values);
|
return new SetCache(values);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user