From 940559fc76f3ffccbc76039731ffd76af3a90c9a Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 3 Oct 2015 16:27:21 -0700 Subject: [PATCH] Optimize `cacheIndexOf`, `cachePush`, and `isKeyable`. --- lodash.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index c4d5e968f..e4963a001 100644 --- a/lodash.js +++ b/lodash.js @@ -1836,7 +1836,14 @@ * @returns {number} Returns `0` if `value` is found, else `-1`. */ function cacheIndexOf(cache, value) { - return cache.__data__.has(value) ? 0 : -1; + var map = cache.__data__; + if (isKeyable(value)) { + var data = map.__data__, + hash = typeof value == 'string' ? data.string : data.hash; + + return hash[value] === HASH_UNDEFINED ? 0 : -1; + } + return map.has(value) ? 0 : -1; } /** @@ -1848,7 +1855,16 @@ * @param {*} value The value to cache. */ function cachePush(value) { - this.__data__.set(value, true); + var map = this.__data__; + if (isKeyable(value)) { + var data = map.__data__, + hash = typeof value == 'string' ? data.string : data.hash; + + hash[value] = HASH_UNDEFINED; + } + else { + map.set(value, HASH_UNDEFINED); + } } /*------------------------------------------------------------------------*/ @@ -4404,7 +4420,9 @@ * @returns {boolean} Returns `true` if `value` is suitable, else `false`. */ function isKeyable(value) { - return !(value === '__proto__' || isObject(value)); + var type = typeof value; + return type == 'number' || type == 'boolean' || + (type == 'string' && value !== '__proto__') || value == null; } /**