feat: several optimizations on minifier #3979
Open
+411
−87
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
vars3. drop or rewrite unused variables
Benchmarks
From https://github.com/privatenumber/minification-benchmarks, benchmark on MacBook Pro M1 Pro (10cores).
--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "react v17.0.2" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 19385 bar [19385,8173,8177,8265,8448,8506,8543,8668,8746]72.13 kB19.39 kB22.87 kB8.17 kB16 ms22.64 kB8.18 kB223 ms23.07 kB8.27 kB130 ms23.60 kB8.45 kB344 ms23.55 kB8.51 kB75 ms23.70 kB8.54 kB70 ms25.03 kB8.67 kB44 ms25.08 kB8.75 kB55 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "moment v2.29.1" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 36231 bar [36231,18568,18687,18690,19119,19308,19334,19569,19683]173.90 kB36.23 kB57.73 kB18.57 kB505 ms58.21 kB18.69 kB27 ms59.14 kB18.69 kB274 ms59.70 kB19.12 kB737 ms59.73 kB19.31 kB70 ms59.82 kB19.33 kB75 ms62.50 kB19.57 kB92 ms63.15 kB19.68 kB121 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "jquery v3.5.1" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 84498 bar [84498,30867,30903,30912,31058,31470,31621,31799,31954]287.63 kB84.50 kB89.15 kB30.87 kB46 ms88.45 kB30.90 kB716 ms89.54 kB30.91 kB366 ms89.55 kB31.06 kB74 ms94.08 kB31.47 kB127 ms94.55 kB31.62 kB153 ms92.10 kB31.80 kB1,254 ms90.07 kB31.95 kB65 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "vue v2.6.12" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 89668 bar [89668,42522,42919,43036,43659,43925,44368,44450,44679]342.15 kB89.67 kB115.50 kB42.52 kB66 ms116.80 kB42.92 kB470 ms113.80 kB43.04 kB970 ms117.61 kB43.66 kB73 ms117.90 kB43.93 kB1,380 ms118.14 kB44.37 kB67 ms126.14 kB44.45 kB157 ms126.58 kB44.68 kB202 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "lodash v4.17.21" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 96690 bar [96690,24686,25156,25186,25503,25862,26062,26200,26221]544.09 kB96.69 kB68.17 kB24.69 kB773 ms69.84 kB25.16 kB62 ms70.67 kB25.19 kB409 ms72.37 kB25.50 kB1,096 ms74.61 kB25.86 kB144 ms72.09 kB26.06 kB75 ms72.48 kB26.20 kB71 ms75.29 kB26.22 kB170 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "d3 v6.3.1" x-axis ["Original",1,2,3,4,5,6,7] y-axis "Gzip size" 0 --> 130686 bar [130686,87016,87207,88087,88319,88669,89156,90800]555.77 kB130.69 kB263.56 kB87.02 kB1,808 ms265.26 kB87.21 kB142 ms267.77 kB88.09 kB1,009 ms275.35 kB88.32 kB314 ms269.54 kB88.67 kB122 ms276.47 kB89.16 kB462 ms270.13 kB90.80 kB96 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "terser v5.30.3" x-axis ["Original",1,2,3,4,5,6,7] y-axis "Gzip size" 0 --> 193763 bar [193763,123095,123334,123482,123612,124428,124609,126706]1.01 MB193.76 kB455.60 kB123.10 kB126 ms451.19 kB123.33 kB1,715 ms458.29 kB123.48 kB932 ms456.94 kB123.61 kB106 ms474.40 kB124.43 kB432 ms472.16 kB124.61 kB361 ms458.89 kB126.71 kB97 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "three v0.124.0" x-axis ["Original",1,2,3,4,5,6,7,8] y-axis "Gzip size" 0 --> 248267 bar [248267,158497,159071,159198,160696,162498,163036,163198,163725]1.25 MB248.27 kB645.27 kB158.50 kB194 ms641.59 kB159.07 kB2,334 ms653.25 kB159.20 kB1,278 ms643.82 kB160.70 kB125 ms648.83 kB162.50 kB8,440 ms674.49 kB163.04 kB467 ms675.50 kB163.20 kB600 ms646.76 kB163.73 kB117 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "victory v35.8.4" x-axis ["Original",1,2,3,4,5,6,7] y-axis "Gzip size" 0 --> 309942 bar [309942,157435,158055,158706,162471,166386,167579,181071]2.13 MB309.94 kB694.78 kB157.44 kB3,094 ms712.61 kB158.06 kB280 ms715.58 kB158.71 kB1,825 ms719.43 kB162.47 kB148 ms759.34 kB166.39 kB756 ms756.53 kB167.58 kB625 ms724.14 kB181.07 kB133 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "echarts v5.1.1" x-axis ["Original",1,2,3,4,5,6,7] y-axis "Gzip size" 0 --> 684611 bar [684611,320267,321987,325222,326944,330736,331412,331563]3.20 MB684.61 kB993.25 kB320.27 kB523 ms1.00 MB321.99 kB2,953 ms1.01 MB325.22 kB198 ms979.10 kB326.94 kB5,788 ms1.07 MB330.74 kB1,340 ms1.07 MB331.41 kB890 ms1.01 MB331.56 kB175 ms+10,000 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "antd v4.16.1" x-axis ["Original",1,2,3,4,5,6,7] y-axis "Gzip size" 0 --> 825175 bar [825175,452625,454517,457786,463719,475480,478572,488279]6.67 MB825.18 kB2.20 MB452.63 kB5,720 ms2.18 MB454.52 kB676 ms2.25 MB457.79 kB3,332 ms2.27 MB463.72 kB256 ms2.43 MB475.48 kB1,591 ms2.42 MB478.57 kB1,266 ms2.31 MB488.28 kB232 ms+10,000 ms--- config: xyChart: width: 720 height: 360 xAxis: labelPadding: 20 yAxis: labelPadding: 10 --- xychart-beta title "typescript v4.9.5" x-axis ["Original",1,2,3,4,5,6] y-axis "Gzip size" 0 --> 1884998 bar [1884998,851747,854211,868230,876535,879301,915495]10.95 MB1.88 MB3.31 MB851.75 kB1,316 ms3.35 MB854.21 kB7,200 ms3.38 MB868.23 kB399 ms3.54 MB876.54 kB2,107 ms3.53 MB879.30 kB2,734 ms3.49 MB915.50 kB351 ms+10,000 ms+10,000 ms