From 5746169aecebf98461ba364f59c7d8ff022fca04 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 2 Oct 2015 21:39:45 -0700 Subject: [PATCH] Replace `createHash` with a `Hash` constructor. --- lodash.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lodash.js b/lodash.js index 8ac8f122e..c4d5e968f 100644 --- a/lodash.js +++ b/lodash.js @@ -48,6 +48,9 @@ /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT = 'Expected a function'; + /** Used to stand-in for `undefined` hash values. */ + var HASH_UNDEFINED = {}; + /** Used as references for various `Number` constants. */ var INFINITY = 1 / 0, MAX_SAFE_INTEGER = 9007199254740991, @@ -1727,9 +1730,9 @@ */ function MapCache() { this.__data__ = { - 'hash': createHash(), + 'hash': new Hash, 'map': Map ? new Map : [], - 'string': createHash() + 'string': new Hash }; } @@ -1816,11 +1819,10 @@ * @param {Array} [values] The values to cache. */ function SetCache(values) { - var length = values ? values.length : 0, - data = this.__data__ = new MapCache; - + var length = values ? values.length : 0; + this.__data__ = new MapCache; while (length--) { - data.set(values[length], true); + this.push(values[length]); } } @@ -1923,9 +1925,7 @@ * @private * @returns {Object} Returns the new hash object. */ - function createHash() { - return nativeCreate ? nativeCreate(null) : {}; - } + function Hash() {} /** * Removes `key` and its value from the hash. @@ -1936,7 +1936,7 @@ * @returns {boolean} Returns `true` if the entry was removed, else `false`. */ function hashDelete(hash, key) { - return (nativeCreate ? key in hash : hasOwnProperty.call(hash, key)) && delete hash[key]; + return hashHas(hash, key) && delete hash[key]; } /** @@ -1948,7 +1948,11 @@ * @returns {*} Returns the entry value. */ function hashGet(hash, key) { - return (nativeCreate || hasOwnProperty.call(hash, key)) ? hash[key] : undefined; + if (nativeCreate) { + var result = hash[key]; + return result === HASH_UNDEFINED ? undefined : result; + } + return hasOwnProperty.call(hash, key) ? hash[key] : undefined; } /** @@ -1960,7 +1964,7 @@ * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`. */ function hashHas(hash, key) { - return nativeCreate ? key in hash : hasOwnProperty.call(hash, key); + return nativeCreate ? hash[key] !== undefined : hasOwnProperty.call(hash, key); } /** @@ -1972,7 +1976,7 @@ * @param {*} value The value to set. */ function hashSet(hash, key, value) { - hash[key] = value; + hash[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value; } /*------------------------------------------------------------------------*/ @@ -12244,6 +12248,9 @@ LazyWrapper.prototype = baseCreate(baseLodash.prototype); LazyWrapper.prototype.constructor = LazyWrapper; + // Avoid inheriting from `Object.prototype` when possible. + Hash.prototype = nativeCreate ? nativeCreate(null) : objectProto; + // Add functions to the `MapCache` cache. MapCache.prototype['delete'] = mapDelete; MapCache.prototype.get = mapGet;