diff options
author | kzc <kzc@users.noreply.github.com> | 2017-05-01 12:10:11 -0400 |
---|---|---|
committer | Alex Lam S.L <alexlamsl@gmail.com> | 2017-05-02 00:10:11 +0800 |
commit | ea9289771b79c273347af72fba024ca29cfa035d (patch) | |
tree | 8bfe0c9e13be06c03eaaf438298eba4abee1c1a8 /lib | |
parent | 2cb55b2ad0119852bc8714401992724d4fdb224d (diff) | |
download | tracifyjs-ea9289771b79c273347af72fba024ca29cfa035d.tar.gz tracifyjs-ea9289771b79c273347af72fba024ca29cfa035d.zip |
improve literal return optimization (#1860)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js index 4e86a307..e5a7af2a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1780,6 +1780,35 @@ merge(Compressor.prototype, { node.DEFMETHOD("has_side_effects", func); }); + // determine if expression is constant + (function(def){ + function all(list) { + for (var i = list.length; --i >= 0;) + if (!list[i].is_constant_expression()) + return false; + return true; + } + def(AST_Node, return_false); + def(AST_Constant, return_true); + def(AST_Unary, function(){ + return this.expression.is_constant_expression(); + }); + def(AST_Binary, function(){ + return this.left.is_constant_expression() && this.right.is_constant_expression(); + }); + def(AST_Array, function(){ + return all(this.elements); + }); + def(AST_Object, function(){ + return all(this.properties); + }); + def(AST_ObjectProperty, function(){ + return this.value.is_constant_expression(); + }); + })(function(node, func){ + node.DEFMETHOD("is_constant_expression", func); + }); + // tell me if a statement aborts function aborts(thing) { return thing && thing.aborts(); @@ -3004,7 +3033,7 @@ merge(Compressor.prototype, { if (exp instanceof AST_Function) { if (exp.body[0] instanceof AST_Return) { var value = exp.body[0].value; - if (!value || value.is_constant()) { + if (!value || value.is_constant_expression()) { var args = self.args.concat(value || make_node(AST_Undefined, self)); return make_sequence(self, args).transform(compressor); } |