@blumintinc/typescript-memoize 1.2.0 → 1.3.0
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/README.md +342 -342
- package/dist/es2015/memoize-decorator.d.ts +10 -10
- package/dist/es2015/memoize-decorator.js +218 -218
- package/dist/es2015/memoize-decorator.js.map +1 -1
- package/dist/memoize-decorator.d.ts +10 -10
- package/dist/memoize-decorator.js +249 -248
- package/dist/memoize-decorator.js.map +1 -1
- package/package.json +65 -65
- package/src/memoize-decorator.ts +251 -250
package/README.md
CHANGED
|
@@ -1,342 +1,342 @@
|
|
|
1
|
-
# typescript-memoize
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/typescript-memoize)
|
|
4
|
-
[](https://raw.githubusercontent.com/darrylhodgins/typescript-memoize/master/LICENSE)
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
A memoize decorator for Typescript.
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
npm install --save typescript-memoize
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Usage:
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
@Memoize(hashFunction?: (...args: any[]) => any)
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
You can use it in several ways:
|
|
22
|
-
|
|
23
|
-
* Memoize a `get` accessor,
|
|
24
|
-
* Memoize a method which takes no parameters,
|
|
25
|
-
* Memoize a method which varies based on its first parameter only,
|
|
26
|
-
* Memoize a method which varies based on some combination of parameters
|
|
27
|
-
* Memoize a method using objects with deep equality comparison (default)
|
|
28
|
-
|
|
29
|
-
You can call memoized methods *within* the same class, too. This could be useful if you want to memoize the return value for an entire data set, and also a filtered or mapped version of that same set.
|
|
30
|
-
|
|
31
|
-
## Memoize a `get` accessor, or a method which takes no parameters
|
|
32
|
-
|
|
33
|
-
These both work the same way. Subsequent calls to a memoized method without parameters, or to a `get` accessor, always return the same value.
|
|
34
|
-
|
|
35
|
-
I generally consider it an anti-pattern for a call to a `get` accessor to trigger an expensive operation. Simply adding `Memoize()` to a `get` allows for seamless lazy-loading.
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
import {Memoize,MemoizeExpiring} from 'typescript-memoize';
|
|
39
|
-
|
|
40
|
-
class SimpleFoo {
|
|
41
|
-
|
|
42
|
-
// Memoize a method without parameters
|
|
43
|
-
@Memoize()
|
|
44
|
-
public getAllTheData() {
|
|
45
|
-
// do some expensive operation to get data
|
|
46
|
-
return data;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Memoize a method and expire the value after some time in milliseconds
|
|
50
|
-
@MemoizeExpiring(5000)
|
|
51
|
-
public getDataForSomeTime() {
|
|
52
|
-
// do some expensive operation to get data
|
|
53
|
-
return data;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Memoize a getter
|
|
57
|
-
@Memoize()
|
|
58
|
-
public get someValue() {
|
|
59
|
-
// do some expensive operation to calculate value
|
|
60
|
-
return value;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
And then we call them from somewhere else in our code:
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
let simpleFoo = new SimpleFoo();
|
|
70
|
-
|
|
71
|
-
// Memoizes a calculated value and returns it:
|
|
72
|
-
let methodVal1 = simpleFoo.getAllTheData();
|
|
73
|
-
|
|
74
|
-
// Returns memoized value
|
|
75
|
-
let methodVal2 = simpleFoo.getAllTheData();
|
|
76
|
-
|
|
77
|
-
// Memoizes (lazy-loads) a calculated value and returns it:
|
|
78
|
-
let getterVal1 = simpleFoo.someValue;
|
|
79
|
-
|
|
80
|
-
// Returns memoized value
|
|
81
|
-
let getterVal2 = simpleFoo.someValue;
|
|
82
|
-
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## Memoize a method which varies based on its first parameter only
|
|
86
|
-
|
|
87
|
-
Subsequent calls to this style of memoized method will always return the same value.
|
|
88
|
-
|
|
89
|
-
I'm not really sure why anyone would use this approach to memoize a method with *more* than one parameter, but it's possible.
|
|
90
|
-
|
|
91
|
-
```typescript
|
|
92
|
-
import {Memoize} from 'typescript-memoize';
|
|
93
|
-
|
|
94
|
-
class ComplicatedFoo {
|
|
95
|
-
|
|
96
|
-
// Memoize a method without parameters (just like the first example)
|
|
97
|
-
@Memoize()
|
|
98
|
-
public getAllTheData() {
|
|
99
|
-
// do some expensive operation to get data
|
|
100
|
-
return data;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Memoize a method with one parameter
|
|
104
|
-
@Memoize()
|
|
105
|
-
public getSomeOfTheData(id: number) {
|
|
106
|
-
let allTheData = this.getAllTheData(); // if you want to!
|
|
107
|
-
// do some expensive operation to get data
|
|
108
|
-
return data;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Memoize a method with multiple parameters
|
|
112
|
-
// Only the first parameter will be used for memoization
|
|
113
|
-
@Memoize()
|
|
114
|
-
public getGreeting(name: string, planet: string) {
|
|
115
|
-
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
We call these methods from somewhere else in our code:
|
|
122
|
-
|
|
123
|
-
```typescript
|
|
124
|
-
let complicatedFoo = new ComplicatedFoo();
|
|
125
|
-
|
|
126
|
-
// Returns calculated value and memoizes it:
|
|
127
|
-
let oneParam1 = complicatedFoo.getSomeOfTheData();
|
|
128
|
-
|
|
129
|
-
// Returns memoized value
|
|
130
|
-
let oneParam2 = complicatedFoo.getSomeOfTheData();
|
|
131
|
-
|
|
132
|
-
// Memoizes a calculated value and returns it:
|
|
133
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
134
|
-
let greeterVal1 = complicatedFoo.getGreeting('Darryl', 'Earth');
|
|
135
|
-
|
|
136
|
-
// Ignores the second parameter, and returns memoized value
|
|
137
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
138
|
-
let greeterVal2 = complicatedFoo.getGreeting('Darryl', 'Mars');
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## Memoize a method which varies based on some combination of parameters
|
|
142
|
-
|
|
143
|
-
Pass in a `hashFunction` which takes the same parameters as your target method, to memoize values based on all parameters, or some other custom logic
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
import {Memoize} from 'typescript-memoize';
|
|
147
|
-
|
|
148
|
-
class MoreComplicatedFoo {
|
|
149
|
-
|
|
150
|
-
// Memoize a method with multiple parameters
|
|
151
|
-
// Memoize will remember values based on keys like: 'name;planet'
|
|
152
|
-
@Memoize((name: string, planet: string) => {
|
|
153
|
-
return name + ';' + planet;
|
|
154
|
-
})
|
|
155
|
-
public getBetterGreeting(name: string, planet: string) {
|
|
156
|
-
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Memoize based on some other logic
|
|
160
|
-
@Memoize(() => {
|
|
161
|
-
return new Date();
|
|
162
|
-
})
|
|
163
|
-
public memoryLeak(greeting: string) {
|
|
164
|
-
return greeting + '!!!!!';
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Memoize also accepts parameters via a single object argument
|
|
168
|
-
@Memoize({
|
|
169
|
-
expiring: 10000, // milliseconds
|
|
170
|
-
hashFunction: (name: string, planet: string) => {
|
|
171
|
-
return name + ';' + planet;
|
|
172
|
-
}
|
|
173
|
-
})
|
|
174
|
-
public getSameBetterGreeting(name: string, planet: string) {
|
|
175
|
-
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
We call these methods from somewhere else in our code. By now you should be getting the idea:
|
|
182
|
-
|
|
183
|
-
```typescript
|
|
184
|
-
let moreComplicatedFoo = new MoreComplicatedFoo();
|
|
185
|
-
|
|
186
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
187
|
-
let greeterVal1 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Earth');
|
|
188
|
-
|
|
189
|
-
// 'Hello, Darryl! Welcome to Mars'
|
|
190
|
-
let greeterVal2 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Mars');
|
|
191
|
-
|
|
192
|
-
// Fill up the computer with useless greetings:
|
|
193
|
-
let greeting = moreComplicatedFoo.memoryLeak('Hello');
|
|
194
|
-
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
## Memoize accepts one or more "tag" strings that allow the cache to be invalidated on command
|
|
198
|
-
|
|
199
|
-
Passing an array with one or more "tag" strings these will allow you to later clear the cache of results associated with methods or the `get`accessors using the `clear()` function.
|
|
200
|
-
|
|
201
|
-
The `clear()` function also requires an array of "tag" strings.
|
|
202
|
-
|
|
203
|
-
```typescript
|
|
204
|
-
import {Memoize} from 'typescript-memoize';
|
|
205
|
-
|
|
206
|
-
class ClearableFoo {
|
|
207
|
-
|
|
208
|
-
// Memoize accepts tags
|
|
209
|
-
@Memoize({ tags: ["foo", "bar"] })
|
|
210
|
-
public getClearableGreeting(name: string, planet: string) {
|
|
211
|
-
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// Memoize accepts tags
|
|
216
|
-
@Memoize({ tags: ["bar"] })
|
|
217
|
-
public getClearableSum(a: number, b: number) {
|
|
218
|
-
return a + b;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
We call these methods from somewhere else in our code.
|
|
225
|
-
|
|
226
|
-
```typescript
|
|
227
|
-
import {clear} from 'typescript-memoize';
|
|
228
|
-
|
|
229
|
-
let clearableFoo = new ClearableFoo();
|
|
230
|
-
|
|
231
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
232
|
-
let greeterVal1 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
233
|
-
|
|
234
|
-
// Ignores the second parameter, and returns memoized value
|
|
235
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
236
|
-
let greeterVal2 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
237
|
-
|
|
238
|
-
// '3'
|
|
239
|
-
let sum1 = clearableFoo.getClearableSum(2, 1);
|
|
240
|
-
|
|
241
|
-
// Ignores the second parameter, and returns memoized value
|
|
242
|
-
// '3'
|
|
243
|
-
let sum2 = clearableFoo.getClearableSum(2, 2);
|
|
244
|
-
|
|
245
|
-
clear(["foo"]);
|
|
246
|
-
|
|
247
|
-
// The memoized values are cleared, return a new value
|
|
248
|
-
// 'Hello, Darryl! Welcome to Mars'
|
|
249
|
-
let greeterVal3 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
// The memoized value is not associated with 'foo' tag, returns memoized value
|
|
253
|
-
// '3'
|
|
254
|
-
let sum3 = clearableFoo.getClearableSum(2, 2);
|
|
255
|
-
|
|
256
|
-
clear(["bar"]);
|
|
257
|
-
|
|
258
|
-
// The memoized values are cleared, return a new value
|
|
259
|
-
// 'Hello, Darryl! Welcome to Earth'
|
|
260
|
-
let greeterVal4 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
// The memoized values are cleared, return a new value
|
|
264
|
-
// '4'
|
|
265
|
-
let sum4 = clearableFoo.getClearableSum(2, 2);
|
|
266
|
-
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## Memoize with deep equality comparison
|
|
270
|
-
|
|
271
|
-
By default, memoization now uses deep equality when comparing objects, which is useful for objects and arrays with the same structure but different references. If needed, you can disable deep equality with the `useDeepEqual: false` option:
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
import {Memoize} from 'typescript-memoize';
|
|
275
|
-
|
|
276
|
-
class DeepEqualityFoo {
|
|
277
|
-
// Uses deep equality comparison by default
|
|
278
|
-
@Memoize()
|
|
279
|
-
public processObject(obj: any) {
|
|
280
|
-
// This will only be called once for objects with the same structure,
|
|
281
|
-
// even if they are different instances
|
|
282
|
-
return expensiveOperation(obj);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Disable deep equality if you want reference equality instead
|
|
286
|
-
@Memoize({
|
|
287
|
-
useDeepEqual: false
|
|
288
|
-
})
|
|
289
|
-
public processObjectWithReferenceEquality(obj: any) {
|
|
290
|
-
// This will be called for each new object reference,
|
|
291
|
-
// even if they have the same structure
|
|
292
|
-
return expensiveOperation(obj);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Combine with other options
|
|
296
|
-
@Memoize({
|
|
297
|
-
expiring: 5000, // expire after 5 seconds
|
|
298
|
-
tags: ["objects"]
|
|
299
|
-
})
|
|
300
|
-
public processComplexObject(obj: any) {
|
|
301
|
-
return expensiveOperation(obj);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
We can call these methods and get memoized results based on deep equality:
|
|
307
|
-
|
|
308
|
-
```typescript
|
|
309
|
-
let deepFoo = new DeepEqualityFoo();
|
|
310
|
-
|
|
311
|
-
// First call with an object
|
|
312
|
-
const result1 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
313
|
-
|
|
314
|
-
// Second call with different object instance but same structure
|
|
315
|
-
// Will return the memoized result without calling the original method again
|
|
316
|
-
const result2 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
317
|
-
|
|
318
|
-
// Call with different object structure
|
|
319
|
-
// Will call the original method again
|
|
320
|
-
const result3 = deepFoo.processObject({ id: 456, name: "Different" });
|
|
321
|
-
|
|
322
|
-
// Deep equality works with complex nested objects too
|
|
323
|
-
const result4 = deepFoo.processComplexObject({
|
|
324
|
-
user: {
|
|
325
|
-
id: 123,
|
|
326
|
-
details: {
|
|
327
|
-
age: 30,
|
|
328
|
-
preferences: ["red", "blue"]
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
// Using reference equality will call the method for each new object
|
|
334
|
-
const refObj = { id: 123, name: "Test" };
|
|
335
|
-
const result5 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
336
|
-
// This will be memoized since it's the same reference
|
|
337
|
-
const result6 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
338
|
-
// This will call the method again since it's a new reference
|
|
339
|
-
const result7 = deepFoo.processObjectWithReferenceEquality({ id: 123, name: "Test" });
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
The deep equality comparison uses the [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal) package for efficient deep equality comparisons.
|
|
1
|
+
# typescript-memoize
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/typescript-memoize)
|
|
4
|
+
[](https://raw.githubusercontent.com/darrylhodgins/typescript-memoize/master/LICENSE)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A memoize decorator for Typescript.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install --save typescript-memoize
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
@Memoize(hashFunction?: (...args: any[]) => any)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You can use it in several ways:
|
|
22
|
+
|
|
23
|
+
* Memoize a `get` accessor,
|
|
24
|
+
* Memoize a method which takes no parameters,
|
|
25
|
+
* Memoize a method which varies based on its first parameter only,
|
|
26
|
+
* Memoize a method which varies based on some combination of parameters
|
|
27
|
+
* Memoize a method using objects with deep equality comparison (default)
|
|
28
|
+
|
|
29
|
+
You can call memoized methods *within* the same class, too. This could be useful if you want to memoize the return value for an entire data set, and also a filtered or mapped version of that same set.
|
|
30
|
+
|
|
31
|
+
## Memoize a `get` accessor, or a method which takes no parameters
|
|
32
|
+
|
|
33
|
+
These both work the same way. Subsequent calls to a memoized method without parameters, or to a `get` accessor, always return the same value.
|
|
34
|
+
|
|
35
|
+
I generally consider it an anti-pattern for a call to a `get` accessor to trigger an expensive operation. Simply adding `Memoize()` to a `get` allows for seamless lazy-loading.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import {Memoize,MemoizeExpiring} from 'typescript-memoize';
|
|
39
|
+
|
|
40
|
+
class SimpleFoo {
|
|
41
|
+
|
|
42
|
+
// Memoize a method without parameters
|
|
43
|
+
@Memoize()
|
|
44
|
+
public getAllTheData() {
|
|
45
|
+
// do some expensive operation to get data
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Memoize a method and expire the value after some time in milliseconds
|
|
50
|
+
@MemoizeExpiring(5000)
|
|
51
|
+
public getDataForSomeTime() {
|
|
52
|
+
// do some expensive operation to get data
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Memoize a getter
|
|
57
|
+
@Memoize()
|
|
58
|
+
public get someValue() {
|
|
59
|
+
// do some expensive operation to calculate value
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
And then we call them from somewhere else in our code:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
let simpleFoo = new SimpleFoo();
|
|
70
|
+
|
|
71
|
+
// Memoizes a calculated value and returns it:
|
|
72
|
+
let methodVal1 = simpleFoo.getAllTheData();
|
|
73
|
+
|
|
74
|
+
// Returns memoized value
|
|
75
|
+
let methodVal2 = simpleFoo.getAllTheData();
|
|
76
|
+
|
|
77
|
+
// Memoizes (lazy-loads) a calculated value and returns it:
|
|
78
|
+
let getterVal1 = simpleFoo.someValue;
|
|
79
|
+
|
|
80
|
+
// Returns memoized value
|
|
81
|
+
let getterVal2 = simpleFoo.someValue;
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Memoize a method which varies based on its first parameter only
|
|
86
|
+
|
|
87
|
+
Subsequent calls to this style of memoized method will always return the same value.
|
|
88
|
+
|
|
89
|
+
I'm not really sure why anyone would use this approach to memoize a method with *more* than one parameter, but it's possible.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import {Memoize} from 'typescript-memoize';
|
|
93
|
+
|
|
94
|
+
class ComplicatedFoo {
|
|
95
|
+
|
|
96
|
+
// Memoize a method without parameters (just like the first example)
|
|
97
|
+
@Memoize()
|
|
98
|
+
public getAllTheData() {
|
|
99
|
+
// do some expensive operation to get data
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Memoize a method with one parameter
|
|
104
|
+
@Memoize()
|
|
105
|
+
public getSomeOfTheData(id: number) {
|
|
106
|
+
let allTheData = this.getAllTheData(); // if you want to!
|
|
107
|
+
// do some expensive operation to get data
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Memoize a method with multiple parameters
|
|
112
|
+
// Only the first parameter will be used for memoization
|
|
113
|
+
@Memoize()
|
|
114
|
+
public getGreeting(name: string, planet: string) {
|
|
115
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
We call these methods from somewhere else in our code:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
let complicatedFoo = new ComplicatedFoo();
|
|
125
|
+
|
|
126
|
+
// Returns calculated value and memoizes it:
|
|
127
|
+
let oneParam1 = complicatedFoo.getSomeOfTheData();
|
|
128
|
+
|
|
129
|
+
// Returns memoized value
|
|
130
|
+
let oneParam2 = complicatedFoo.getSomeOfTheData();
|
|
131
|
+
|
|
132
|
+
// Memoizes a calculated value and returns it:
|
|
133
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
134
|
+
let greeterVal1 = complicatedFoo.getGreeting('Darryl', 'Earth');
|
|
135
|
+
|
|
136
|
+
// Ignores the second parameter, and returns memoized value
|
|
137
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
138
|
+
let greeterVal2 = complicatedFoo.getGreeting('Darryl', 'Mars');
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Memoize a method which varies based on some combination of parameters
|
|
142
|
+
|
|
143
|
+
Pass in a `hashFunction` which takes the same parameters as your target method, to memoize values based on all parameters, or some other custom logic
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import {Memoize} from 'typescript-memoize';
|
|
147
|
+
|
|
148
|
+
class MoreComplicatedFoo {
|
|
149
|
+
|
|
150
|
+
// Memoize a method with multiple parameters
|
|
151
|
+
// Memoize will remember values based on keys like: 'name;planet'
|
|
152
|
+
@Memoize((name: string, planet: string) => {
|
|
153
|
+
return name + ';' + planet;
|
|
154
|
+
})
|
|
155
|
+
public getBetterGreeting(name: string, planet: string) {
|
|
156
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Memoize based on some other logic
|
|
160
|
+
@Memoize(() => {
|
|
161
|
+
return new Date();
|
|
162
|
+
})
|
|
163
|
+
public memoryLeak(greeting: string) {
|
|
164
|
+
return greeting + '!!!!!';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Memoize also accepts parameters via a single object argument
|
|
168
|
+
@Memoize({
|
|
169
|
+
expiring: 10000, // milliseconds
|
|
170
|
+
hashFunction: (name: string, planet: string) => {
|
|
171
|
+
return name + ';' + planet;
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
public getSameBetterGreeting(name: string, planet: string) {
|
|
175
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
We call these methods from somewhere else in our code. By now you should be getting the idea:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
let moreComplicatedFoo = new MoreComplicatedFoo();
|
|
185
|
+
|
|
186
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
187
|
+
let greeterVal1 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Earth');
|
|
188
|
+
|
|
189
|
+
// 'Hello, Darryl! Welcome to Mars'
|
|
190
|
+
let greeterVal2 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Mars');
|
|
191
|
+
|
|
192
|
+
// Fill up the computer with useless greetings:
|
|
193
|
+
let greeting = moreComplicatedFoo.memoryLeak('Hello');
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Memoize accepts one or more "tag" strings that allow the cache to be invalidated on command
|
|
198
|
+
|
|
199
|
+
Passing an array with one or more "tag" strings these will allow you to later clear the cache of results associated with methods or the `get`accessors using the `clear()` function.
|
|
200
|
+
|
|
201
|
+
The `clear()` function also requires an array of "tag" strings.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import {Memoize} from 'typescript-memoize';
|
|
205
|
+
|
|
206
|
+
class ClearableFoo {
|
|
207
|
+
|
|
208
|
+
// Memoize accepts tags
|
|
209
|
+
@Memoize({ tags: ["foo", "bar"] })
|
|
210
|
+
public getClearableGreeting(name: string, planet: string) {
|
|
211
|
+
return 'Hello, ' + name + '! Welcome to ' + planet;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
// Memoize accepts tags
|
|
216
|
+
@Memoize({ tags: ["bar"] })
|
|
217
|
+
public getClearableSum(a: number, b: number) {
|
|
218
|
+
return a + b;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
We call these methods from somewhere else in our code.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import {clear} from 'typescript-memoize';
|
|
228
|
+
|
|
229
|
+
let clearableFoo = new ClearableFoo();
|
|
230
|
+
|
|
231
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
232
|
+
let greeterVal1 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
233
|
+
|
|
234
|
+
// Ignores the second parameter, and returns memoized value
|
|
235
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
236
|
+
let greeterVal2 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
237
|
+
|
|
238
|
+
// '3'
|
|
239
|
+
let sum1 = clearableFoo.getClearableSum(2, 1);
|
|
240
|
+
|
|
241
|
+
// Ignores the second parameter, and returns memoized value
|
|
242
|
+
// '3'
|
|
243
|
+
let sum2 = clearableFoo.getClearableSum(2, 2);
|
|
244
|
+
|
|
245
|
+
clear(["foo"]);
|
|
246
|
+
|
|
247
|
+
// The memoized values are cleared, return a new value
|
|
248
|
+
// 'Hello, Darryl! Welcome to Mars'
|
|
249
|
+
let greeterVal3 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
// The memoized value is not associated with 'foo' tag, returns memoized value
|
|
253
|
+
// '3'
|
|
254
|
+
let sum3 = clearableFoo.getClearableSum(2, 2);
|
|
255
|
+
|
|
256
|
+
clear(["bar"]);
|
|
257
|
+
|
|
258
|
+
// The memoized values are cleared, return a new value
|
|
259
|
+
// 'Hello, Darryl! Welcome to Earth'
|
|
260
|
+
let greeterVal4 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
// The memoized values are cleared, return a new value
|
|
264
|
+
// '4'
|
|
265
|
+
let sum4 = clearableFoo.getClearableSum(2, 2);
|
|
266
|
+
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Memoize with deep equality comparison
|
|
270
|
+
|
|
271
|
+
By default, memoization now uses deep equality when comparing objects, which is useful for objects and arrays with the same structure but different references. If needed, you can disable deep equality with the `useDeepEqual: false` option:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import {Memoize} from 'typescript-memoize';
|
|
275
|
+
|
|
276
|
+
class DeepEqualityFoo {
|
|
277
|
+
// Uses deep equality comparison by default
|
|
278
|
+
@Memoize()
|
|
279
|
+
public processObject(obj: any) {
|
|
280
|
+
// This will only be called once for objects with the same structure,
|
|
281
|
+
// even if they are different instances
|
|
282
|
+
return expensiveOperation(obj);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Disable deep equality if you want reference equality instead
|
|
286
|
+
@Memoize({
|
|
287
|
+
useDeepEqual: false
|
|
288
|
+
})
|
|
289
|
+
public processObjectWithReferenceEquality(obj: any) {
|
|
290
|
+
// This will be called for each new object reference,
|
|
291
|
+
// even if they have the same structure
|
|
292
|
+
return expensiveOperation(obj);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Combine with other options
|
|
296
|
+
@Memoize({
|
|
297
|
+
expiring: 5000, // expire after 5 seconds
|
|
298
|
+
tags: ["objects"]
|
|
299
|
+
})
|
|
300
|
+
public processComplexObject(obj: any) {
|
|
301
|
+
return expensiveOperation(obj);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
We can call these methods and get memoized results based on deep equality:
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
let deepFoo = new DeepEqualityFoo();
|
|
310
|
+
|
|
311
|
+
// First call with an object
|
|
312
|
+
const result1 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
313
|
+
|
|
314
|
+
// Second call with different object instance but same structure
|
|
315
|
+
// Will return the memoized result without calling the original method again
|
|
316
|
+
const result2 = deepFoo.processObject({ id: 123, name: "Test" });
|
|
317
|
+
|
|
318
|
+
// Call with different object structure
|
|
319
|
+
// Will call the original method again
|
|
320
|
+
const result3 = deepFoo.processObject({ id: 456, name: "Different" });
|
|
321
|
+
|
|
322
|
+
// Deep equality works with complex nested objects too
|
|
323
|
+
const result4 = deepFoo.processComplexObject({
|
|
324
|
+
user: {
|
|
325
|
+
id: 123,
|
|
326
|
+
details: {
|
|
327
|
+
age: 30,
|
|
328
|
+
preferences: ["red", "blue"]
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// Using reference equality will call the method for each new object
|
|
334
|
+
const refObj = { id: 123, name: "Test" };
|
|
335
|
+
const result5 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
336
|
+
// This will be memoized since it's the same reference
|
|
337
|
+
const result6 = deepFoo.processObjectWithReferenceEquality(refObj);
|
|
338
|
+
// This will call the method again since it's a new reference
|
|
339
|
+
const result7 = deepFoo.processObjectWithReferenceEquality({ id: 123, name: "Test" });
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
The deep equality comparison uses the [fast-deep-equal](https://www.npmjs.com/package/fast-deep-equal) package for efficient deep equality comparisons.
|