You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.ts
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -37,18 +37,18 @@ export type Options<
37
37
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible:
38
38
39
39
```
40
-
importmem from 'mem';
40
+
importmemoize from 'memoize';
41
41
42
-
mem(function_, {cacheKey: JSON.stringify});
42
+
memoize(function_, {cacheKey: JSON.stringify});
43
43
```
44
44
45
45
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on.
46
46
47
47
```
48
-
importmem from 'mem';
48
+
importmemoize from 'memoize';
49
49
import serializeJavascript from 'serialize-javascript';
Copy file name to clipboardExpand all lines: readme.md
+39-28Lines changed: 39 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#mem
1
+
#memoize
2
2
3
3
>[Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
4
4
@@ -13,17 +13,17 @@ If you want to memoize Promise-returning functions (like `async` functions), you
13
13
##Install
14
14
15
15
```sh
16
-
npm installmem
16
+
npm installmemoize
17
17
```
18
18
19
19
##Usage
20
20
21
21
```js
22
-
importmemfrom'mem';
22
+
importmemoizefrom'memoize';
23
23
24
24
let index=0;
25
25
constcounter= ()=>++index;
26
-
constmemoized=mem(counter);
26
+
constmemoized=memoize(counter);
27
27
28
28
memoized('foo');
29
29
//=> 1
@@ -49,11 +49,11 @@ memoized('bar', 'foo');
49
49
But you might want to use[p-memoize](https://github.com/sindresorhus/p-memoize) for more Promise-specific behaviors.
heavyMemoizedOperation({full:true});// Stored in cache with the object as key
116
122
heavyMemoizedOperation({full:true});// Stored in cache with the object as key, again
@@ -120,7 +126,9 @@ heavyMemoizedOperation({full: true}); // Stored in cache with the object as key,
120
126
You might want to serialize or hash them, for example using`JSON.stringify` or something like[serialize-javascript](https://github.com/yahoo/serialize-javascript), which can also serialize`RegExp`,`Date` and so on.
If your function accepts multiple arguments that aren't supported by`JSON.stringify` (e.g. DOM elements and functions), you can instead extend the initial exact equality (`===`) to work on multiple arguments using[`many-keys-map`](https://github.com/fregante/many-keys-map):
// The cacheKey returns an array. This isn't deduplicated by a regular Map, but it's valid. The correct solution would be to use ManyKeysMap to deduplicate it correctly