0
# Compression
1
2
Advanced JavaScript code optimization system providing dead code elimination, constant folding, expression simplification, and 75+ individual optimization passes for maximum code size reduction.
3
4
## Capabilities
5
6
### Compression Options
7
8
Comprehensive optimization configuration with fine-grained control over individual optimization techniques.
9
10
```typescript { .api }
11
interface CompressOptions {
12
/** Optimize function arguments and parameters */
13
arguments?: boolean;
14
/** Convert function expressions to arrow functions when safe */
15
arrows?: boolean;
16
/** Convert boolean values to integers (true=1, false=0) */
17
booleans_as_integers?: boolean;
18
/** Optimize boolean expressions and conditions */
19
booleans?: boolean;
20
/** Collapse single-use variables into their usage sites */
21
collapse_vars?: boolean;
22
/** Optimize comparison operations (==, !=, <, >, etc.) */
23
comparisons?: boolean;
24
/** Optimize computed property access to dot notation */
25
computed_props?: boolean;
26
/** Optimize conditional expressions and ternary operators */
27
conditionals?: boolean;
28
/** Remove unreachable code (dead code elimination) */
29
dead_code?: boolean;
30
/** Apply default set of safe optimizations */
31
defaults?: boolean;
32
/** Remove or optimize directive prologues ("use strict", etc.) */
33
directives?: boolean;
34
/** Remove console method calls */
35
drop_console?: DropConsoleOption;
36
/** Remove debugger statements */
37
drop_debugger?: boolean;
38
/** Target ECMAScript version for optimizations */
39
ecma?: ECMA;
40
/** Evaluate and fold constant expressions */
41
evaluate?: boolean;
42
/** Treat input as single expression rather than program */
43
expression?: boolean;
44
/** Global constant definitions for conditional compilation */
45
global_defs?: object;
46
/** Hoist function declarations to top of scope */
47
hoist_funs?: boolean;
48
/** Hoist property assignments out of expressions */
49
hoist_props?: boolean;
50
/** Hoist var declarations to top of function */
51
hoist_vars?: boolean;
52
/** Enable Internet Explorer 8 compatibility mode */
53
ie8?: boolean;
54
/** Optimize if-return statements */
55
if_return?: boolean;
56
/** Inline function calls and expressions */
57
inline?: boolean | InlineFunctions;
58
/** Join consecutive var declarations */
59
join_vars?: boolean;
60
/** Preserve class names during optimization */
61
keep_classnames?: boolean | RegExp;
62
/** Preserve function arguments in functions marked for removal */
63
keep_fargs?: boolean;
64
/** Preserve function names during optimization */
65
keep_fnames?: boolean | RegExp;
66
/** Keep Infinity literal instead of 1/0 */
67
keep_infinity?: boolean;
68
/** Optimize assignments to left-hand side constants */
69
lhs_constants?: boolean;
70
/** Optimize loop conditions and structures */
71
loops?: boolean;
72
/** Treat code as ES6 module */
73
module?: boolean;
74
/** Convert function expressions to negated IIFEs */
75
negate_iife?: boolean;
76
/** Number of compression passes to perform */
77
passes?: number;
78
/** Optimize object property access and assignment */
79
properties?: boolean;
80
/** Function names safe to remove when return value is not used */
81
pure_funcs?: string[];
82
/** Constructor functions safe to optimize away */
83
pure_new?: boolean;
84
/** Property getters that are side-effect free */
85
pure_getters?: boolean | 'strict';
86
/** Inline and remove function declarations when beneficial */
87
reduce_funcs?: boolean;
88
/** Optimize variable assignments and reduce variable usage */
89
reduce_vars?: boolean;
90
/** Join statements with comma operator */
91
sequences?: boolean | number;
92
/** Drop statements that have no side effects */
93
side_effects?: boolean;
94
/** Optimize switch statements */
95
switches?: boolean;
96
/** Apply optimizations to top-level scope */
97
toplevel?: boolean;
98
/** Variable names to retain in top-level scope */
99
top_retain?: null | string | string[] | RegExp;
100
/** Optimize typeof expressions */
101
typeofs?: boolean;
102
/** Enable unsafe arrow function conversions */
103
unsafe_arrows?: boolean;
104
/** Enable potentially unsafe optimizations */
105
unsafe?: boolean;
106
/** Unsafe comparison optimizations */
107
unsafe_comps?: boolean;
108
/** Optimize Function constructor calls (unsafe) */
109
unsafe_Function?: boolean;
110
/** Optimize Math object method calls (unsafe) */
111
unsafe_math?: boolean;
112
/** Optimize symbol-related operations (unsafe) */
113
unsafe_symbols?: boolean;
114
/** Optimize method calls and property access (unsafe) */
115
unsafe_methods?: boolean;
116
/** Optimize prototype chain operations (unsafe) */
117
unsafe_proto?: boolean;
118
/** Optimize regular expression literals (unsafe) */
119
unsafe_regexp?: boolean;
120
/** Optimize undefined comparisons (unsafe) */
121
unsafe_undefined?: boolean;
122
/** Remove unused variables and functions */
123
unused?: boolean;
124
}
125
```
126
127
### Dead Code Elimination
128
129
Comprehensive dead code removal including unreachable statements, unused variables, and side-effect-free expressions.
130
131
**Usage Examples:**
132
133
```typescript
134
// Aggressive dead code removal
135
const result = await minify(code, {
136
compress: {
137
dead_code: true, // Remove unreachable code
138
unused: true, // Remove unused variables
139
side_effects: false, // Remove side-effect-free statements
140
pure_funcs: [ // Functions safe to remove
141
'console.log',
142
'console.info',
143
'debug'
144
]
145
}
146
});
147
148
// Conservative dead code removal
149
const result = await minify(code, {
150
compress: {
151
dead_code: true,
152
unused: false, // Keep unused variables (safer)
153
side_effects: true // Preserve all side effects
154
}
155
});
156
```
157
158
### Constant Folding and Evaluation
159
160
Compile-time expression evaluation and constant propagation for optimal code reduction.
161
162
**Usage Examples:**
163
164
```typescript
165
// Enable aggressive constant folding
166
const result = await minify(code, {
167
compress: {
168
evaluate: true, // Fold constant expressions
169
conditionals: true, // Optimize if/else with constants
170
booleans: true, // Optimize boolean expressions
171
loops: true, // Unroll loops with constant bounds
172
global_defs: { // Define compile-time constants
173
DEBUG: false,
174
API_VERSION: '"1.2.3"',
175
MAX_ITEMS: 100
176
}
177
}
178
});
179
180
// Before: if (DEBUG) { console.log('debug info'); }
181
// After: (code removed entirely)
182
```
183
184
### Console and Debugging
185
186
Control console method handling and debugging statement removal.
187
188
**Usage Examples:**
189
190
```typescript
191
// Remove all console calls
192
await minify(code, {
193
compress: {
194
drop_console: true,
195
drop_debugger: true
196
}
197
});
198
199
// Selective console removal
200
await minify(code, {
201
compress: {
202
drop_console: ['log', 'info'], // Keep console.error, console.warn
203
drop_debugger: true
204
}
205
});
206
207
// Production vs development builds
208
const prodOptions = {
209
compress: {
210
drop_console: true,
211
drop_debugger: true,
212
pure_funcs: ['console.log', 'assert']
213
}
214
};
215
216
const devOptions = {
217
compress: {
218
drop_console: false, // Keep console calls
219
drop_debugger: false // Keep debugger statements
220
}
221
};
222
```
223
224
### Multi-pass Optimization
225
226
Configure multiple optimization passes for maximum compression with diminishing returns analysis.
227
228
**Usage Examples:**
229
230
```typescript
231
// Single pass (fastest)
232
const result = await minify(code, {
233
compress: { passes: 1 }
234
});
235
236
// Multiple passes (better compression)
237
const result = await minify(code, {
238
compress: {
239
passes: 3, // 3 optimization passes
240
sequences: 200, // Longer sequence limits
241
conditionals: true, // Multi-pass conditional optimization
242
booleans: true // Multi-pass boolean optimization
243
}
244
});
245
```
246
247
### Unsafe Optimizations
248
249
Advanced optimizations that may break code in edge cases but provide significant size reduction.
250
251
**Usage Examples:**
252
253
```typescript
254
// Conservative unsafe optimizations
255
const result = await minify(code, {
256
compress: {
257
unsafe: false, // Disable most unsafe opts
258
unsafe_math: true, // Only Math optimizations
259
unsafe_arrows: false // Keep original function forms
260
}
261
});
262
263
// Aggressive unsafe optimizations
264
const result = await minify(code, {
265
compress: {
266
unsafe: true, // Enable unsafe optimizations
267
unsafe_comps: true, // Unsafe comparisons
268
unsafe_Function: true, // Optimize Function() calls
269
unsafe_math: true, // Optimize Math methods
270
unsafe_proto: true, // Optimize prototype access
271
unsafe_regexp: true, // Optimize RegExp literals
272
unsafe_undefined: true // Optimize undefined usage
273
}
274
});
275
```
276
277
## Types
278
279
```typescript { .api }
280
type ConsoleProperty = keyof typeof console;
281
type DropConsoleOption = boolean | ConsoleProperty[];
282
283
enum InlineFunctions {
284
Disabled = 0,
285
SimpleFunctions = 1,
286
WithArguments = 2,
287
WithArgumentsAndVariables = 3
288
}
289
290
type ECMA = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020;
291
```