-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-memoize-workpad.mjs
379 lines (312 loc) · 12.2 KB
/
proxy-memoize-workpad.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import memoize from 'proxy-memoize'
const state = {
objects: {
object1: {
rank: 1,
color: 'red'
},
object2: {
rank: 4,
color: 'green'
},
object3: {
rank: 2,
color: 'yellow'
},
object4: {
rank: 3,
color: 'blue'
}
},
toggler: true
}
// memoize sorter such that it does not need to re-run
// unless the rank of one of the selected objects has changed.
let objectList = ['object4', 'object3', 'object1']
let selectObjectSorterWithOnlyState = memoize(({state}) => {
console.log('generating sorter')
return (objA, objB) => { console.log('running sorter'); return state.objects[objA].rank - state.objects[objB].rank; }
});
let sortedWithOnlyStateMemoized = [...objectList].sort(selectObjectSorterWithOnlyState({state}))
// > generating sorter
// > running sorter
// > running sorter
console.log(sortedWithOnlyStateMemoized)
// > [ 'object1', 'object3', 'object4' ]
let _sortedAgainWithOnlyStateMemoized = [...objectList].sort(selectObjectSorterWithOnlyState({state}))
// > running sorter
// > running sorter
// So that doesn't meaningfully impact the sort at all
// What if the sorter is memoized as well?
let selectSortResultWithStateAndInputsMemoized = memoize(({state, valA, valB}) => {
console.log('running sorter')
return state.objects[valA].rank - state.objects[valB].rank;
}, 500)
let sortedWithStateAndInputsMemoized = [...objectList].sort((valA,valB) => selectSortResultWithStateAndInputsMemoized({state, valA, valB }))
// > running sorter
// > running sorter
let _sortedAgainWithStateAndInputsMemoized = [...objectList].sort((valA, valB) => selectSortResultWithStateAndInputsMemoized({state, valA, valB}))
// > running sorter
// > running sorter
// So, weirdly, that didn't work at all. Even though we're providing the same inputs -- the same state, valA, and valB --
// the memoization did not stop the sort evaluator function from being called.
// Let's try trusting the magic a bit more.
let runcount = 0
let selectSorted = memoize(({state}) => {
console.log('running sorter ', runcount++)
return [...state.objectList].sort((valA, valB) => { console.log('running sort pass'); return state.objects[valA].rank - state.objects[valB].rank; })
})
console.log(selectSorted({state}))
// > running sort pass
// > running sort pass
// > running sorter 0
// > (3) ["object1", "object3", "object4"]
console.log(selectSorted({state}))
// > (3) ["object1", "object3", "object4"]
// ok, so that avoided the whole second sort. what if we change one of the objects and re-run?
state = Object.assign({},state, {
objects: {
...state.objects,
object3: {
...state.object3,
rank: 4
}
}
})
console.log(selectSorted({state}))
// > running sorter 1
// > running sort pass
// > running sort pass
// > running sort pass
// > running sort pass
// > (3) ["object1", "object4", "object3"]
// great -- as expected, the sort re-ran. interestingly, but not relatedly, it took two more passes with the items in the new order.
// what if we change one of the other objects but not ones we're referring to?
state = Object.assign({},state, {
objects: {
...state.objects,
object2: {
...state.object2,
rank: 2
}
}
})
console.log(selectSorted({state}))
// > (3) ["object1", "object4", "object3"]
// cool -- sort did not re-run. what if we change one of the values in one of the objects we're interested in, but not rank?
state = Object.assign({},state, {
objects: {
...state.objects,
object3: {
...state.objects.object3,
color: 'mauve'
}
}
})
console.log(selectSorted({state}))
// > (3) ["object1", "object4", "object3"]
// okay, same -- no new sort. so we can save the running of the sort algorithm if the items to sort have not changed.
// but if one of the values changes, then the sort has to re-run in full.
// what if the list changes?
// let's leave the values the same but the array new.
state = Object.assign({},state, {
objectList: ['object1', 'object3', 'object4']
})
console.log(selectSorted({state}))
// > running sorter 2
// > running sort pass
// > running sort pass
// > running sort pass
// > running sort pass
// > (3) ["object1", "object4", "object3"]
// ok, so that triggered a complete re-sort.
// presumably a new item in the list will do the same thing.
state = Object.assign({},state, {
objectList: ['object1', 'object3', 'object4', 'object2']
})
console.log(selectSorted({state}))
// > running sort pass
// > running sort pass
// > running sort pass
// > running sort pass
// > running sort pass
// > running sort pass
// > (4) ["object1", "object2", "object4", "object3"]
// yep.
// so it's weird though that we can't cache the inner function of the sorter.
// let's try one more time.
let innerSort = 0
const selectSortComparison = memoize(({state, objectA, objectB}) => {
console.log('running inner sorter ', innerSort++)
return state.objects[objectA].rank - state.objects[objectB].rank
})
console.log(selectSortComparison({state, objectA: 'object1', objectB: 'object2'}))
// > running inner sorter 0
// > -1
console.log(selectSortComparison({state, objectA: 'object1', objectB: 'object2'}))
// > -1
// so no, that does actually work. so let's go a step further..
let outerSort = 0
const selectSortedObjects = memoize(({state}) => {
console.log('running outer sorter ', outerSort++)
return [...state.objectList].sort((objectA, objectB) => selectSortComparison({state, objectA, objectB}))
})
console.log(selectSortedObjects({state}));
// > running outer sorter 0
// > running inner sorter 0
// > running inner sorter 1
// > running inner sorter 2
// > running inner sorter 3
// > running inner sorter 4
// (4) ["object1", "object2", "object4", "object3"]
console.log(selectSortedObjects({state}));
// (4) ["object1", "object2", "object4", "object3"]
// okay, so that did work because we're using the cached value of the sorter function. but what happens if we remove an object from the list?
state = Object.assign({},state, {
objectList: ['object1', 'object3', 'object4']
})
console.log(selectSortedObjects({state}));
// > running outer sorter 1
// > running inner sorter 5
// > running inner sorter 6
// > running inner sorter 7
// > (3) ["object1", "object4", "object3"]
// so it seems to have run again. what if we restore the original values in state?
state = Object.assign({},state, {
objectList: [...state.objectList, 'object2']
})
console.log(selectSortedObjects({state}));
// > running outer sorter 2
// > running inner sorter 8
// > running inner sorter 9
// > running inner sorter 10
// > running inner sorter 11
// > running inner sorter 12
// > (4) ["object1", "object2", "object4", "object3"]
// so even though the inputs to the inner sort function are the same by value, it's still not saving us any runs.
// let's try extending the 'size' of the sort function.
innerSort = 0
outerSort = 0
const selectSortComparison2 = memoize(({state, objectA, objectB}) => {
console.log('running inner sorter ', innerSort++)
return state.objects[objectA].rank - state.objects[objectB].rank
}, { size: 500 })
const selectSortedObjects2 = memoize(({state}) => {
console.log('running outer sorter ', outerSort++)
return [...state.objectList].sort((objectA, objectB) => selectSortComparison2({state, objectA, objectB}))
})
console.log(selectSortedObjects2({state}));
// > running outer sorter 0
// > running inner sorter 0
// > running inner sorter 1
// > running inner sorter 2
// > running inner sorter 3
// > running inner sorter 4
// (4) ["object1", "object2", "object4", "object3"]
console.log(selectSortedObjects2({state}));
// (4) ["object1", "object2", "object4", "object3"]
// No change. Let's continue:
console.log(selectSortedObjects2({state}));
state = Object.assign({},state, {
objectList: ['object1', 'object3', 'object4']
})
console.log(selectSortedObjects2({state}));
// > running outer sorter 1
// > running inner sorter 5
// > running inner sorter 6
// > running inner sorter 7
// > (3) ["object1", "object4", "object3"]
// oof. So it's again 3 comparisons -- even though the input objects haven't changed.
// maybe the problem isn't the state object but the object object that we're passing into the sort.
// let's try again. but first for completion's sake let's restore the object to its previous state, as
// we did before, and see if the result is still 3 runs:
state = Object.assign({},state, {
objectList: [...state.objectList, 'object2']
})
console.log(selectSortedObjects2({state}));
// > running outer sorter 2
// > running inner sorter 8
// > running inner sorter 9
// > running inner sorter 10
// > running inner sorter 11
// > running inner sorter 12
// > (4) ["object1", "object2", "object4", "object3"]
// ok -- so setting the size parameter on the inner sort didn't change a damn thing.
// let's try changing the inner sort parameters to point directly to the objects in state.
innerSort = 0
outerSort = 0
const selectSortComparison3 = memoize(({objectA, objectB}) => {
console.log('running inner sorter ', innerSort++)
return objectA.rank - objectB.rank
}, { size: 500 })
const selectSortedObjects3 = memoize(({state}) => {
console.log('running outer sorter ', outerSort++)
return [...state.objectList].sort((objectA, objectB) => selectSortComparison3({objectA: state.objects[objectA], objectB: state.objects[objectB]}))
})
console.log(selectSortedObjects3({state}));
console.log(selectSortedObjects3({state}));
// > running outer sorter 0
// > running inner sorter 0
// > running inner sorter 1
// > running inner sorter 2
// > running inner sorter 3
// > running inner sorter 4
// > (4) ["object1", "object2", "object4", "object3"]
// > (4) ["object1", "object2", "object4", "object3"]
// So, as expected: it takes 5 runs through the sort the array, and the outer memoization proxy prevents from having to run again with the state completely unchanged.
// Now let's retry our mutations.
state = Object.assign({},state, {
objectList: ['object1', 'object3', 'object4']
})
console.log(selectSortedObjects3({state}));
// > running outer sorter 1
// > (3) ["object1", "object4", "object3"]
// Aha! Brilliant. Now the sorter is re-run because the state has changed, but the inner comparators don't need to re-run at all.
// What if we now extend the array?
state = Object.assign({},state, {
objectList: [...state.objectList, 'object2']
})
console.log(selectSortedObjects3({state}));
// > running outer sorter
// >> (4) ["object1", "object2", "object4", "object3"]
// Hell yes. Now even if we extend the array, it doesn't have to re-run the comparisons. It's actually caching based on the objects' inner values. Wow.
// OK, so let's try some of the object mutation and see how the results change.
state = Object.assign({},state, {
objects: {
...state.objects,
object2: {
...state.object2,
rank: 5
}
}
})
console.log(selectSortedObjects3({state}))
// > running outer sorter 3
// > running inner sorter 5
// > running inner sorter 6
// > (4) ["object1", "object4", "object3", "object2"]
// OK -- so this time it ran the inner sorter twice, instead of five times.
// OK, so now what happens if we change the toggle on the state? None of our objects are involved.
state = Object.assign({},state, {
toggler: false
})
console.log(selectSortedObjects3({state}))
// > (4) ["object1", "object4", "object3", "object2"]
// OK -- so that's interesting. It's safe to pass the whole state; as long as you're not touching part of it, it won't re-render. So why did passing objectNames
// not work before? Because they were strings. **The memoization only works by reference**?
// Wait, let's duplicate that early one more precisely.
//
// let selectSortResultWithStateAndInputsMemoized = memoize(({state, valA, valB}) => {
// console.log('running sorter')
// return state.objects[valA].rank - state.objects[valB].rank;
// }, 500)
innerSort = 0
outerSort = 0
const selectSortComparison4 = memoize(({state, objectIdA, objectIdB}) => {
console.log('running inner sorter ', innerSort++)
return state.objects[objectIdA].rank - state.objects[objectIdB].rank
}, { size: 500 })
const selectSortedObjects4 = memoize(({state}) => {
console.log('running outer sorter ', outerSort++)
return [...state.objectList].sort((objectA, objectB) => selectSortComparison3({objectA: state.objects[objectA], objectB: state.objects[objectB]}))
})