-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathupdate-with-same-state-m2m.js
65 lines (49 loc) · 1.81 KB
/
update-with-same-state-m2m.js
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
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
const REMOVAL_COUNT = 10;
export default Route.extend({
store: service(),
async model() {
performance.mark('start-data-generation');
const initialPayload = await fetch('./fixtures/big-many-to-many.json').then((r) => r.json());
const initialPayload2 = structuredClone(initialPayload);
const payloadWithRemoval = await fetch('./fixtures/big-many-to-many-with-removal.json').then((r) => r.json());
performance.mark('start-push-initial-payload');
this.store.push(initialPayload);
performance.mark('start-peek-records');
const peekedCars = this.store.peekAll('car');
const peekedColors = this.store.peekAll('color');
performance.mark('start-record-materialization');
peekedColors.slice();
peekedCars.slice();
performance.mark('start-relationship-materialization');
const seen = new Set();
peekedCars.forEach((car) => iterateCar(car, seen));
const removedColors = [];
performance.mark('start-local-removal');
for (const car of peekedCars) {
const colors = car.colors;
removedColors.push(colors.splice(0, REMOVAL_COUNT));
}
performance.mark('start-push-minus-one-payload');
this.store.push(payloadWithRemoval);
performance.mark('start-local-addition');
peekedCars.forEach((car, index) => {
car.colors = removedColors[index].concat(car.colors);
});
performance.mark('start-push-plus-one-payload');
this.store.push(initialPayload2);
performance.mark('end-push-plus-one-payload');
},
});
function iterateChild(record, seen) {
if (seen.has(record)) {
return;
}
seen.add(record);
record.cars;
}
function iterateCar(record, seen) {
seen.add(record);
record.colors.forEach((color) => iterateChild(color, seen));
}