@chriscdn/memoize 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +30 -18
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -18,21 +18,22 @@ yarn add @chriscdn/memoize
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
The package
|
|
21
|
+
The package provides two functions: `Memoize` and `MemoizeAsync`.
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
import { Memoize, MemoizeAsync } from "@chriscdn/memoize";
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
- **`Memoize`** is used to memoize a _synchronous_ function.
|
|
28
|
+
- **`MemoizeAsync`** is used to memoize an _asynchronous_ function.
|
|
28
29
|
|
|
29
|
-
Each call to `Memoize` or `MemoizeAsync`
|
|
30
|
+
The cache is powered by [quick-lru](https://www.npmjs.com/package/quick-lru). Each call to `Memoize` or `MemoizeAsync` creates a new cache instance.
|
|
30
31
|
|
|
31
|
-
The `MemoizeAsync` function prevents duplicate evaluations by ensuring multiple calls with
|
|
32
|
+
The `MemoizeAsync` function prevents duplicate evaluations by ensuring that multiple calls with the same cache key are processed only once.
|
|
32
33
|
|
|
33
|
-
By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This can be customized (see below).
|
|
34
|
+
By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This behavior can be customized (see below).
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
### Example (Synchronous)
|
|
36
37
|
|
|
37
38
|
To memoize a function:
|
|
38
39
|
|
|
@@ -49,9 +50,15 @@ const result = add(5, 7);
|
|
|
49
50
|
// 12
|
|
50
51
|
```
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
You can also define the function in a single line:
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
```ts
|
|
56
|
+
const add = Memoize((x: number, y: number) => x + y);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Example (Asynchronous)
|
|
60
|
+
|
|
61
|
+
Memoizing an asynchronous function is similar:
|
|
55
62
|
|
|
56
63
|
```ts
|
|
57
64
|
const _add = async (x: number, y: number) => x + y;
|
|
@@ -64,44 +71,47 @@ const result = await add(5, 7);
|
|
|
64
71
|
|
|
65
72
|
## Options
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
Both `Memoize` and `MemoizeAsync` accept an `options` parameter to control the cache behavior:
|
|
68
75
|
|
|
69
76
|
```ts
|
|
70
77
|
const add = MemoizeAsync(_add, options);
|
|
71
78
|
```
|
|
72
79
|
|
|
73
|
-
|
|
80
|
+
Available options (with defaults):
|
|
74
81
|
|
|
75
82
|
```ts
|
|
76
83
|
const options = {
|
|
77
|
-
//
|
|
84
|
+
// Maximum number of items in the cache
|
|
78
85
|
maxSize: 1000,
|
|
79
86
|
|
|
80
|
-
//
|
|
87
|
+
// Maximum lifespan of an item in milliseconds; undefined means items never expire
|
|
81
88
|
maxAge: undefined,
|
|
82
89
|
|
|
83
|
-
//
|
|
90
|
+
// A synchronous function to generate cache keys (must return a string)
|
|
84
91
|
resolver: (...args) => JSON.stringify(args),
|
|
85
92
|
};
|
|
86
93
|
```
|
|
87
94
|
|
|
88
95
|
## Cache
|
|
89
96
|
|
|
90
|
-
The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance
|
|
97
|
+
The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance is accessible via the `.cache` property on the memoized function:
|
|
91
98
|
|
|
92
99
|
```ts
|
|
93
100
|
const add = MemoizeAsync(_add);
|
|
94
101
|
const result = await add(5, 7);
|
|
95
102
|
|
|
96
|
-
console.log(
|
|
103
|
+
console.log(add.cache.size === 1);
|
|
97
104
|
// true
|
|
105
|
+
|
|
106
|
+
// Clear the cache
|
|
107
|
+
add.cache.clear();
|
|
98
108
|
```
|
|
99
109
|
|
|
100
110
|
## Class Methods
|
|
101
111
|
|
|
102
|
-
Class methods can also be memoized,
|
|
112
|
+
Class methods can also be memoized, but this requires overriding the method within the constructor. Ensure you bind the method to the instance to maintain the correct context.
|
|
103
113
|
|
|
104
|
-
|
|
114
|
+
Here's an example where the `add` method is memoized by reassigning it within the constructor. This approach preserves access to instance properties (like `count`) and maintains the correct method signature in TypeScript:
|
|
105
115
|
|
|
106
116
|
```ts
|
|
107
117
|
class AddClass {
|
|
@@ -118,10 +128,12 @@ class AddClass {
|
|
|
118
128
|
}
|
|
119
129
|
```
|
|
120
130
|
|
|
121
|
-
|
|
131
|
+
Each memoized method in each class instance maintains its own cache.
|
|
122
132
|
|
|
123
133
|
## Tests
|
|
124
134
|
|
|
135
|
+
Run the tests using:
|
|
136
|
+
|
|
125
137
|
```bash
|
|
126
138
|
yarn test
|
|
127
139
|
```
|
package/package.json
CHANGED