speed up IIFE elimination (#1728)

- `side_effects` will clean up inner statements, so checking for an empty function body should suffice
- drop side effects when dropping `return` from statement
This commit is contained in:
Alex Lam S.L
2017-03-29 23:27:35 +08:00
committed by GitHub
parent f1a833a7aa
commit beb9659778

View File

@@ -199,7 +199,7 @@ merge(Compressor.prototype, {
return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string(); return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string();
}); });
AST_Node.DEFMETHOD("process_expression", function(insert) { AST_Node.DEFMETHOD("process_expression", function(insert, compressor) {
var self = this; var self = this;
var tt = new TreeTransformer(function(node) { var tt = new TreeTransformer(function(node) {
if (insert && node instanceof AST_SimpleStatement) { if (insert && node instanceof AST_SimpleStatement) {
@@ -208,6 +208,12 @@ merge(Compressor.prototype, {
}); });
} }
if (!insert && node instanceof AST_Return) { if (!insert && node instanceof AST_Return) {
if (compressor) {
var value = node.value && node.value.drop_side_effect_free(compressor, true);
return value ? make_node(AST_SimpleStatement, node, {
body: value
}) : make_node(AST_EmptyStatement, node);
}
return make_node(AST_SimpleStatement, node, { return make_node(AST_SimpleStatement, node, {
body: node.value || make_node(AST_Undefined, node) body: node.value || make_node(AST_Undefined, node)
}); });
@@ -2153,7 +2159,7 @@ merge(Compressor.prototype, {
if (this.expression instanceof AST_Function if (this.expression instanceof AST_Function
&& (!this.expression.name || !this.expression.name.definition().references.length)) { && (!this.expression.name || !this.expression.name.definition().references.length)) {
var node = this.clone(); var node = this.clone();
node.expression = node.expression.process_expression(false); node.expression = node.expression.process_expression(false, compressor);
return node; return node;
} }
return this; return this;
@@ -2866,11 +2872,9 @@ merge(Compressor.prototype, {
return AST_Seq.from_array(args).transform(compressor); return AST_Seq.from_array(args).transform(compressor);
} }
} }
if (compressor.option("side_effects")) { if (compressor.option("side_effects") && all(exp.body, is_empty)) {
if (!AST_Block.prototype.has_side_effects.call(exp, compressor)) { var args = self.args.concat(make_node(AST_Undefined, self));
var args = self.args.concat(make_node(AST_Undefined, self)); return AST_Seq.from_array(args).transform(compressor);
return AST_Seq.from_array(args).transform(compressor);
}
} }
} }
if (compressor.option("drop_console")) { if (compressor.option("drop_console")) {