1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-19 05:31:56 +02:00

Only lookup entry once on memoize cache hit

This commit is contained in:
Soreine
2016-11-01 13:19:39 +01:00
parent fea1e69e72
commit 136867d240

View File

@@ -11,6 +11,13 @@ import { Map } from 'immutable'
const LEAF = {}
/**
* An unique value used to detect cache misses
*
* @type {Object}
*/
const NO_SET = {}
/**
* Memoize all of the `properties` on a `object`.
*
@@ -31,7 +38,10 @@ function memoize(object, properties) {
const keys = [property, ...args, LEAF]
const cache = this.__cache = this.__cache || new Map()
if (cache.hasIn(keys)) return cache.getIn(keys)
const cachedValue = cache.getIn(keys, NO_SET)
if (cachedValue !== NO_SET) {
return cachedValue
}
const value = original.apply(this, args)
this.__cache = cache.setIn(keys, value)