+184
-5
lines changedFilter options
+184
-5
lines changed Original file line number Diff line number Diff line change
@@ -348,6 +348,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
348
348
- `collapse_vars` -- default `false`. Collapse single-use `var` and `const`
349
349
definitions when possible.
350
350
351
+
- `reduce_vars` -- default `false`. Improve optimization on variables assigned
352
+
with and used as constant values.
353
+
351
354
- `warnings` -- display warnings when dropping unreachable code or unused
352
355
declarations etc.
353
356
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ function Compressor(options, false_by_default) {
67
67
if_return : !false_by_default,
68
68
join_vars : !false_by_default,
69
69
collapse_vars : false,
70
+
reduce_vars : false,
70
71
cascade : !false_by_default,
71
72
side_effects : !false_by_default,
72
73
pure_getters : false,
@@ -1107,7 +1108,7 @@ merge(Compressor.prototype, {
1107
1108
this._evaluating = true;
1108
1109
try {
1109
1110
var d = this.definition();
1110
-
if (d && d.constant && d.init) {
1111
+
if (d && (d.constant || compressor.option("reduce_vars") && !d.modified) && d.init) {
1111
1112
return ev(d.init, compressor);
1112
1113
}
1113
1114
} finally {
Original file line number Diff line number Diff line change
@@ -197,7 +197,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
197
197
}
198
198
if (node instanceof AST_SymbolRef) {
199
199
var name = node.name;
200
-
if (name == "eval" && tw.parent() instanceof AST_Call) {
200
+
var parent = tw.parent();
201
+
if (name == "eval" && parent instanceof AST_Call) {
201
202
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
202
203
s.uses_eval = true;
203
204
}
@@ -213,12 +214,15 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
213
214
g.global = true;
214
215
globals.set(name, g);
215
216
}
216
-
node.thedef = g;
217
+
sym = g;
217
218
if (func && name == "arguments") {
218
219
func.uses_arguments = true;
219
220
}
220
-
} else {
221
-
node.thedef = sym;
221
+
}
222
+
node.thedef = sym;
223
+
if (parent instanceof AST_Unary && (parent.operator === '++' || parent.operator === '--')
224
+
|| parent instanceof AST_Assign && parent.left === node) {
225
+
sym.modified = true;
222
226
}
223
227
node.reference();
224
228
return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
1
+
reduce_vars: {
2
+
options = {
3
+
conditionals : true,
4
+
evaluate : true,
5
+
global_defs : {
6
+
C : 0
7
+
},
8
+
reduce_vars : true,
9
+
unused : true
10
+
}
11
+
input: {
12
+
var A = 1;
13
+
(function f0() {
14
+
var a = 2;
15
+
console.log(a - 5);
16
+
console.log(A - 5);
17
+
})();
18
+
(function f1() {
19
+
var a = 2;
20
+
console.log(a - 5);
21
+
eval("console.log(a);");
22
+
})();
23
+
(function f2(eval) {
24
+
var a = 2;
25
+
console.log(a - 5);
26
+
eval("console.log(a);");
27
+
})(eval);
28
+
(function f3() {
29
+
var b = typeof C !== "undefined";
30
+
var c = 4;
31
+
if (b) {
32
+
return 'yes';
33
+
} else {
34
+
return 'no';
35
+
}
36
+
})();
37
+
console.log(A + 1);
38
+
}
39
+
expect: {
40
+
var A = 1;
41
+
(function() {
42
+
console.log(-3);
43
+
console.log(-4);
44
+
})();
45
+
(function f1() {
46
+
var a = 2;
47
+
console.log(-3);
48
+
eval("console.log(a);");
49
+
})();
50
+
(function f2(eval) {
51
+
var a = 2;
52
+
console.log(-3);
53
+
eval("console.log(a);");
54
+
})(eval);
55
+
(function() {
56
+
return "yes";
57
+
})();
58
+
console.log(2);
59
+
}
60
+
}
61
+
62
+
modified: {
63
+
options = {
64
+
conditionals : true,
65
+
evaluate : true,
66
+
reduce_vars : true,
67
+
unused : true
68
+
}
69
+
input: {
70
+
function f0() {
71
+
var a = 1, b = 2;
72
+
b++;
73
+
console.log(a + 1);
74
+
console.log(b + 1);
75
+
}
76
+
77
+
function f1() {
78
+
var a = 1, b = 2;
79
+
--b;
80
+
console.log(a + 1);
81
+
console.log(b + 1);
82
+
}
83
+
84
+
function f2() {
85
+
var a = 1, b = 2, c = 3;
86
+
b = c;
87
+
console.log(a + b);
88
+
console.log(b + c);
89
+
console.log(a + c);
90
+
console.log(a + b + c);
91
+
}
92
+
93
+
function f3() {
94
+
var a = 1, b = 2, c = 3;
95
+
b *= c;
96
+
console.log(a + b);
97
+
console.log(b + c);
98
+
console.log(a + c);
99
+
console.log(a + b + c);
100
+
}
101
+
102
+
function f4() {
103
+
var a = 1, b = 2, c = 3;
104
+
if (a) {
105
+
b = c;
106
+
} else {
107
+
c = b;
108
+
}
109
+
console.log(a + b);
110
+
console.log(b + c);
111
+
// TODO: as "modified" is determined in "figure_out_scope",
112
+
// even "passes" wouldn't improve this any further
113
+
console.log(a + c);
114
+
console.log(a + b + c);
115
+
}
116
+
117
+
function f5(a) {
118
+
B = a;
119
+
console.log(A ? 'yes' : 'no');
120
+
console.log(B ? 'yes' : 'no');
121
+
}
122
+
}
123
+
expect: {
124
+
function f0() {
125
+
var b = 2;
126
+
b++;
127
+
console.log(2);
128
+
console.log(b + 1);
129
+
}
130
+
131
+
function f1() {
132
+
var b = 2;
133
+
--b;
134
+
console.log(2);
135
+
console.log(b + 1);
136
+
}
137
+
138
+
function f2() {
139
+
var a = 1, b = 2, c = 3;
140
+
b = c;
141
+
console.log(a + b);
142
+
console.log(b + c);
143
+
console.log(4);
144
+
console.log(a + b + c);
145
+
}
146
+
147
+
function f3() {
148
+
var a = 1, b = 2, c = 3;
149
+
b *= c;
150
+
console.log(a + b);
151
+
console.log(b + c);
152
+
console.log(4);
153
+
console.log(a + b + c);
154
+
}
155
+
156
+
function f4() {
157
+
var a = 1, b = 2, c = 3;
158
+
b = c;
159
+
console.log(a + b);
160
+
console.log(b + c);
161
+
console.log(a + c);
162
+
console.log(a + b + c);
163
+
}
164
+
165
+
function f5(a) {
166
+
B = a;
167
+
console.log(A ? 'yes' : 'no');
168
+
console.log(B ? 'yes' : 'no');
169
+
}
170
+
}
171
+
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4