@samitouri / QOS-React-2 / commits / f7f7a88e19

Example suboptimal memoization with non-inlined useMemo

Joe Savona committed Apr 7, 2023 at 16:34 UTC f7f7a88e19674e6dec8c60ec22601ef347113636
2 files changed +70
compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.expect.md new
+61
@@ -0,0 +1,61 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const [a, b] = useMemo(() => {
7 + const items = [];
8 + const a = makeObject(props.a);
9 + const b = makeObject(props.b);
10 + return [a, b];
11 + });
12 + return [a, b];
13 +}
14 +
15 +```
16 +
17 +## Code
18 +
19 +```javascript
20 +function Component(props) {
21 + const $ = React.unstable_useMemoCache(7);
22 + const c_0 = $[0] !== props.a;
23 + let t0;
24 + if (c_0) {
25 + t0 = () => {
26 + const items = [];
27 + const a = makeObject(props.a);
28 + const b = makeObject(props.b);
29 + return [a, b];
30 + };
31 + $[0] = props.a;
32 + $[1] = t0;
33 + } else {
34 + t0 = $[1];
35 + }
36 + const c_2 = $[2] !== t0;
37 + let t1;
38 + if (c_2) {
39 + t1 = t0();
40 + $[2] = t0;
41 + $[3] = t1;
42 + } else {
43 + t1 = $[3];
44 + }
45 + const [a_0, b_0] = t1;
46 + const c_4 = $[4] !== a_0;
47 + const c_5 = $[5] !== b_0;
48 + let t2;
49 + if (c_4 || c_5) {
50 + t2 = [a_0, b_0];
51 + $[4] = a_0;
52 + $[5] = b_0;
53 + $[6] = t2;
54 + } else {
55 + t2 = $[6];
56 + }
57 + return t2;
58 +}
59 +
60 +```
61 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/useMemo-independently-memoizeable.js new
+9
@@ -0,0 +1,9 @@
1 +function Component(props) {
2 + const [a, b] = useMemo(() => {
3 + const items = [];
4 + const a = makeObject(props.a);
5 + const b = makeObject(props.b);
6 + return [a, b];
7 + });
8 + return [a, b];
9 +}