Skip to content

Commit

Permalink
refactor: resultCache (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Jan 15, 2024
1 parent 551ae15 commit 16ef457
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,24 @@ export function memoize<Obj extends object, Result>(
[AFFECTED_PROPERTY]: Affected;
}
const memoList: Entry[] = [];
const resultCache = options?.noWeakMap ? null : new WeakMap<Obj, Entry>();
const resultCache = options?.noWeakMap ? null : new WeakMap<Obj, Result>();
const memoizedFn = (obj: Obj) => {
const cache = resultCache?.get(obj);
if (cache) {
return cache[RESULT_PROPERTY];
return cache;
}
for (let i = 0; i < size; i += 1) {
const memo = memoList[(memoListHead + i) % size];
if (!memo) break;
if (!isChanged(
memo[OBJ_PROPERTY],
obj,
memo[AFFECTED_PROPERTY],
new WeakMap(),
isOriginalEqual,
)) {
touchAffected(obj, memo[OBJ_PROPERTY], memo[AFFECTED_PROPERTY]);
resultCache?.set(obj, memo);
return memo[RESULT_PROPERTY];
const {
[OBJ_PROPERTY]: memoObj,
[AFFECTED_PROPERTY]: memoAffected,
[RESULT_PROPERTY]: memoResult,
} = memo;
if (!isChanged(memoObj, obj, memoAffected, new WeakMap(), isOriginalEqual)) {
touchAffected(obj, memoObj, memoAffected);
resultCache?.set(obj, memoResult);
return memoResult;
}
}
const affected: Affected = new WeakMap();
Expand All @@ -134,7 +133,7 @@ export function memoize<Obj extends object, Result>(
};
memoListHead = (memoListHead - 1 + size) % size;
memoList[memoListHead] = entry;
resultCache?.set(obj, entry);
resultCache?.set(obj, result);
return result;
};
return memoizedFn;
Expand Down

0 comments on commit 16ef457

Please sign in to comment.