From beb9659778b04e46556b58bd1d093f3938be9dce Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 29 Mar 2017 23:27:35 +0800 Subject: 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 --- lib/compress.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/compress.js b/lib/compress.js index 786fc567..e36ff893 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -199,7 +199,7 @@ merge(Compressor.prototype, { 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 tt = new TreeTransformer(function(node) { if (insert && node instanceof AST_SimpleStatement) { @@ -208,6 +208,12 @@ merge(Compressor.prototype, { }); } 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, { body: node.value || make_node(AST_Undefined, node) }); @@ -2153,7 +2159,7 @@ merge(Compressor.prototype, { if (this.expression instanceof AST_Function && (!this.expression.name || !this.expression.name.definition().references.length)) { var node = this.clone(); - node.expression = node.expression.process_expression(false); + node.expression = node.expression.process_expression(false, compressor); return node; } return this; @@ -2866,11 +2872,9 @@ merge(Compressor.prototype, { return AST_Seq.from_array(args).transform(compressor); } } - if (compressor.option("side_effects")) { - if (!AST_Block.prototype.has_side_effects.call(exp, compressor)) { - var args = self.args.concat(make_node(AST_Undefined, self)); - return AST_Seq.from_array(args).transform(compressor); - } + if (compressor.option("side_effects") && all(exp.body, is_empty)) { + var args = self.args.concat(make_node(AST_Undefined, self)); + return AST_Seq.from_array(args).transform(compressor); } } if (compressor.option("drop_console")) { -- cgit v1.2.3