mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 06:01:24 +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 = {}
|
const LEAF = {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An unique value used to detect cache misses
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
const NO_SET = {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Memoize all of the `properties` on a `object`.
|
* Memoize all of the `properties` on a `object`.
|
||||||
*
|
*
|
||||||
@@ -31,7 +38,10 @@ function memoize(object, properties) {
|
|||||||
const keys = [property, ...args, LEAF]
|
const keys = [property, ...args, LEAF]
|
||||||
const cache = this.__cache = this.__cache || new Map()
|
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)
|
const value = original.apply(this, args)
|
||||||
this.__cache = cache.setIn(keys, value)
|
this.__cache = cache.setIn(keys, value)
|
||||||
|
Reference in New Issue
Block a user