aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorMihai Bazon <mihai@bazon.net>2013-12-29 10:31:30 +0200
committerMihai Bazon <mihai@bazon.net>2013-12-29 10:31:30 +0200
commitb521b4b926f7a385ed14ec07bc4b5dad9ebcd93b (patch)
tree5a1a75c0b9efb50a87cb6c6e4919911bd81db064 /lib/compress.js
parentaa9de76370ee83883f19a6d410ad8207d0fe1769 (diff)
downloadtracifyjs-b521b4b926f7a385ed14ec07bc4b5dad9ebcd93b.tar.gz
tracifyjs-b521b4b926f7a385ed14ec07bc4b5dad9ebcd93b.zip
Conditional/call optimization
foo ? bar(x) : bar(y) ==> bar(foo ? x : y)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a3ba16ab..9a6dd5e5 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2219,7 +2219,7 @@ merge(Compressor.prototype, {
* ==>
* exp = foo ? something : something_else;
*/
- self = make_node(AST_Assign, self, {
+ return make_node(AST_Assign, self, {
operator: consequent.operator,
left: consequent.left,
right: make_node(AST_Conditional, self, {
@@ -2229,6 +2229,25 @@ merge(Compressor.prototype, {
})
});
}
+ if (consequent instanceof AST_Call
+ && alternative.TYPE === consequent.TYPE
+ && consequent.args.length == alternative.args.length
+ && consequent.expression.equivalent_to(alternative.expression)) {
+ if (consequent.args.length == 0) {
+ return make_node(AST_Seq, self, {
+ car: self.condition,
+ cdr: consequent
+ });
+ }
+ if (consequent.args.length == 1) {
+ consequent.args[0] = make_node(AST_Conditional, self, {
+ condition: self.condition,
+ consequent: consequent.args[0],
+ alternative: alternative.args[0]
+ });
+ return consequent;
+ }
+ }
return self;
});