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:
@@ -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)
|
||||
|
Reference in New Issue
Block a user