feat: several optimizations on minifier #3979
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
Our platform provides approximately hundreds of thousands of frontend builds each month for both internal and external developers. To balance machine resources and the size of the artifacts, we have adopted esbuild and optimized its minification strategy (details can be found in PRs from two years ago like #2614). These optimizations reduced the package size inflation rate compared to terser from 15% before optimization to around 4%.
Our use case primarily involved minifying ES5 code, so the previous implementation did indeed have some issues that were not exposed in our production environment. Earlier this year, I re-implemented the optimizations, which is why the old PR was closed and this PR comes.
Optimizations
1. reuse names for non-overlapping symbols
2. allow inline
var
s3. drop or rewrite unused variables
Benchmarks
From https://github.com/privatenumber/minification-benchmarks, benchmark on MacBook Pro M1 Pro (10cores).
72.13 kB
19.39 kB
22.87 kB
8.17 kB
16 ms
22.64 kB
8.18 kB
223 ms
23.07 kB
8.27 kB
130 ms
23.60 kB
8.45 kB
344 ms
23.55 kB
8.51 kB
75 ms
23.70 kB
8.54 kB
70 ms
25.03 kB
8.67 kB
44 ms
25.08 kB
8.75 kB
55 ms
173.90 kB
36.23 kB
57.73 kB
18.57 kB
505 ms
58.21 kB
18.69 kB
27 ms
59.14 kB
18.69 kB
274 ms
59.70 kB
19.12 kB
737 ms
59.73 kB
19.31 kB
70 ms
59.82 kB
19.33 kB
75 ms
62.50 kB
19.57 kB
92 ms
63.15 kB
19.68 kB
121 ms
287.63 kB
84.50 kB
89.15 kB
30.87 kB
46 ms
88.45 kB
30.90 kB
716 ms
89.54 kB
30.91 kB
366 ms
89.55 kB
31.06 kB
74 ms
94.08 kB
31.47 kB
127 ms
94.55 kB
31.62 kB
153 ms
92.10 kB
31.80 kB
1,254 ms
90.07 kB
31.95 kB
65 ms
342.15 kB
89.67 kB
115.50 kB
42.52 kB
66 ms
116.80 kB
42.92 kB
470 ms
113.80 kB
43.04 kB
970 ms
117.61 kB
43.66 kB
73 ms
117.90 kB
43.93 kB
1,380 ms
118.14 kB
44.37 kB
67 ms
126.14 kB
44.45 kB
157 ms
126.58 kB
44.68 kB
202 ms
544.09 kB
96.69 kB
68.17 kB
24.69 kB
773 ms
69.84 kB
25.16 kB
62 ms
70.67 kB
25.19 kB
409 ms
72.37 kB
25.50 kB
1,096 ms
74.61 kB
25.86 kB
144 ms
72.09 kB
26.06 kB
75 ms
72.48 kB
26.20 kB
71 ms
75.29 kB
26.22 kB
170 ms
555.77 kB
130.69 kB
263.56 kB
87.02 kB
1,808 ms
265.26 kB
87.21 kB
142 ms
267.77 kB
88.09 kB
1,009 ms
275.35 kB
88.32 kB
314 ms
269.54 kB
88.67 kB
122 ms
276.47 kB
89.16 kB
462 ms
270.13 kB
90.80 kB
96 ms
1.01 MB
193.76 kB
455.60 kB
123.10 kB
126 ms
451.19 kB
123.33 kB
1,715 ms
458.29 kB
123.48 kB
932 ms
456.94 kB
123.61 kB
106 ms
474.40 kB
124.43 kB
432 ms
472.16 kB
124.61 kB
361 ms
458.89 kB
126.71 kB
97 ms
1.25 MB
248.27 kB
645.27 kB
158.50 kB
194 ms
641.59 kB
159.07 kB
2,334 ms
653.25 kB
159.20 kB
1,278 ms
643.82 kB
160.70 kB
125 ms
648.83 kB
162.50 kB
8,440 ms
674.49 kB
163.04 kB
467 ms
675.50 kB
163.20 kB
600 ms
646.76 kB
163.73 kB
117 ms
2.13 MB
309.94 kB
694.78 kB
157.44 kB
3,094 ms
712.61 kB
158.06 kB
280 ms
715.58 kB
158.71 kB
1,825 ms
719.43 kB
162.47 kB
148 ms
759.34 kB
166.39 kB
756 ms
756.53 kB
167.58 kB
625 ms
724.14 kB
181.07 kB
133 ms
3.20 MB
684.61 kB
993.25 kB
320.27 kB
523 ms
1.00 MB
321.99 kB
2,953 ms
1.01 MB
325.22 kB
198 ms
979.10 kB
326.94 kB
5,788 ms
1.07 MB
330.74 kB
1,340 ms
1.07 MB
331.41 kB
890 ms
1.01 MB
331.56 kB
175 ms
+10,000 ms
6.67 MB
825.18 kB
2.20 MB
452.63 kB
5,720 ms
2.18 MB
454.52 kB
676 ms
2.25 MB
457.79 kB
3,332 ms
2.27 MB
463.72 kB
256 ms
2.43 MB
475.48 kB
1,591 ms
2.42 MB
478.57 kB
1,266 ms
2.31 MB
488.28 kB
232 ms
+10,000 ms
10.95 MB
1.88 MB
3.31 MB
851.75 kB
1,316 ms
3.35 MB
854.21 kB
7,200 ms
3.38 MB
868.23 kB
399 ms
3.54 MB
876.54 kB
2,107 ms
3.53 MB
879.30 kB
2,734 ms
3.49 MB
915.50 kB
351 ms
+10,000 ms
+10,000 ms