@samitouri / QOS-React-1 / commits / 4fe0ab41e3

Repro for unnecessary memoization of array filter

Joe Savona committed Oct 12, 2023 at 12:03 UTC 4fe0ab41e323acb990cace942ef26630b42ac0d1
2 files changed +100
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.expect.md new
+77
@@ -0,0 +1,77 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Component(props) {
6 + const filtered = props.items.filter((item) => item != null);
7 + return filtered;
8 +}
9 +
10 +export const FIXTURE_ENTRYPOINT = {
11 + fn: Component,
12 + params: [
13 + {
14 + items: [
15 + { a: true },
16 + null,
17 + true,
18 + false,
19 + null,
20 + "string",
21 + 3.14,
22 + null,
23 + [null],
24 + ],
25 + },
26 + ],
27 +};
28 +
29 +```
30 +
31 +## Code
32 +
33 +```javascript
34 +import { unstable_useMemoCache as useMemoCache } from "react";
35 +function Component(props) {
36 + const $ = useMemoCache(3);
37 + const c_0 = $[0] !== props.items;
38 + let t1;
39 + if (c_0) {
40 + let t0;
41 + if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
42 + t0 = (item) => item != null;
43 + $[2] = t0;
44 + } else {
45 + t0 = $[2];
46 + }
47 + t1 = props.items.filter(t0);
48 + $[0] = props.items;
49 + $[1] = t1;
50 + } else {
51 + t1 = $[1];
52 + }
53 + const filtered = t1;
54 + return filtered;
55 +}
56 +
57 +export const FIXTURE_ENTRYPOINT = {
58 + fn: Component,
59 + params: [
60 + {
61 + items: [
62 + { a: true },
63 + null,
64 + true,
65 + false,
66 + null,
67 + "string",
68 + 3.14,
69 + null,
70 + [null],
71 + ],
72 + },
73 + ],
74 +};
75 +
76 +```
77 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/noAlias-filter-on-array-prop.js new
+23
@@ -0,0 +1,23 @@
1 +function Component(props) {
2 + const filtered = props.items.filter((item) => item != null);
3 + return filtered;
4 +}
5 +
6 +export const FIXTURE_ENTRYPOINT = {
7 + fn: Component,
8 + params: [
9 + {
10 + items: [
11 + { a: true },
12 + null,
13 + true,
14 + false,
15 + null,
16 + "string",
17 + 3.14,
18 + null,
19 + [null],
20 + ],
21 + },
22 + ],
23 +};