Skip to content

Commit

Permalink
docs: Document memoizing dictionary objects
Browse files Browse the repository at this point in the history
Addresses #120
  • Loading branch information
medikoo authored Oct 19, 2023
1 parent 05677ae commit b8e93ed
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ Still above two methods do not serve all cases, e.g. if we want to memoize funct

##### Writing custom cache id normalizers

There's a `normalizer` option through which we can pass custom cache id normalization function
e.g. if we want to memoize a function where argument is a hash object which we do not want to compare by instance but by its content, then we can achieve it as following:
There's a `normalizer` option through which we can pass custom cache id normalization function.

###### Memoizing on dictionary (object hash) arguments)

if we want to memoize a function where argument is a hash object which we do not want to compare by instance but by its content, then we can achieve it as following:

```javascript
var mfn = memoize(
Expand All @@ -135,6 +138,33 @@ mfn({ foo: "bar" });
mfn({ foo: "bar" }); // Cache hit
```

If additionally we want to ensure that our logic works well with different order of properties, we need to imply an additional sorting, e.g.

```
const deepSortedEntries = object =>
Object.entries(object)
.map(([key, value]) => {
if (value && typeof value === "object") return [key, deepSortedEntries(value)];
return [key, value];
})
.sort();
var mfn = memoize(
function(hash) {
// body of memoized function
},
{
normalizer: function(args) {
// args is arguments object as accessible in memoized function
return JSON.stringify(deepSortedEntries(args[0]));
}
}
);
mfn({ foo: "bar", bar: "foo" });
mfn({ bar: "foo", foo: "bar" }); // Cache hit
```

#### Argument resolvers

When we're expecting arguments of certain type it's good to coerce them before doing memoization. We can do that by passing additional resolvers array:
Expand Down

0 comments on commit b8e93ed

Please sign in to comment.