Skip to content

Commit

Permalink
add weakMemoizeObj
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed May 6, 2019
1 parent e7f0ad0 commit c78198a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/objectTrie.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {weakMemoizeArray} from "./weakMemoize";
import {weakMemoizeObj} from "./weakMemoize";

export const EDGE = 'EDGE';

export const pushObjTrie = (root, line) => {
const pushObjTrie = (root, line) => {
const path = line.split('.');
let node = root;
const lastIndex = path.length - 1;
Expand All @@ -24,4 +24,4 @@ const buildObjTrie = (lines) => {
return root;
};

export const memoizedBuildTrie = weakMemoizeArray(buildObjTrie);
export const memoizedBuildTrie = weakMemoizeObj(buildObjTrie);
14 changes: 13 additions & 1 deletion src/weakMemoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export const weakMemoizeArray = fn => {
}
}

export const weakMemoizeObj = fn => {
let cache = new WeakMap();
return arg => {
if (cache.has(arg)) {
return cache.get(arg);
}
const ret = fn(arg);
cache.set(arg, ret);
return ret;
};
};

export const weakMemoizeWalk = fn => {
let cache = new WeakMap();
return (a, b, node) => {
Expand All @@ -35,5 +47,5 @@ export const weakMemoizeWalk = fn => {
b,
});
return ret;
}
};
};

0 comments on commit c78198a

Please sign in to comment.