From c662ff0eb57d396e4cebd55ae08cbe50b1a97c31 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Sat, 13 Sep 2014 18:59:19 +0300 Subject: [PATCH 1/3] Optimize conditionals where the condition and consequent are equivalent and have no side effects --- lib/compress.js | 9 +++++++++ test/compress/conditionals.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index 77de5946..f6177743 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2332,6 +2332,15 @@ merge(Compressor.prototype, { } } + // x=y?y:z --> x=y||z + if (self.condition.equivalent_to(consequent) + && !self.condition.has_side_effects(compressor)) { + return make_node(AST_Binary, self.condition, { + left: self.condition, + operator: "||", + right: alternative + }); + } return self; }); diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index c20297aa..ab4f2512 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -293,3 +293,38 @@ cond_7: { } } +cond_8: { + options = { + conditionals: true, + evaluate : true + }; + input: { + // compress these + a = condition ? condition : b; + + if (condition) { + a = condition; + } else { + a = b; + } + + a = condition ? condition : b(); + + // Don't compress conditions that have side effects + if (condition()) { + a = condition(); + } else { + a = b; + } + + a = condition() ? condition() : b; + + } + expect: { + a = condition || b; + a = condition || b; + a = condition || b(); + a = condition() ? condition() : b; + a = condition() ? condition() : b; + } +} \ No newline at end of file From cd448923019342cf4cb759aa1da4ea7701728d80 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Sat, 13 Sep 2014 19:47:10 +0300 Subject: [PATCH 2/3] Optimize conditionals where the consequent and alternative are both booleans and not equivalent --- lib/compress.js | 14 ++++++++++++ test/compress/conditionals.js | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index f6177743..ac5d0d50 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2341,6 +2341,20 @@ merge(Compressor.prototype, { right: alternative }); } + // x=y?true:false --> x=!!y + if (consequent instanceof AST_True + && alternative instanceof AST_False) { + self.condition = self.condition.negate(compressor); + return make_node(AST_UnaryPrefix, self.condition, { + operator: "!", + expression: self.condition + }); + } + // x=y?false:true --> x=!y + if (consequent instanceof AST_False + && alternative instanceof AST_True) { + return self.condition.negate(compressor) + } return self; }); diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index ab4f2512..ce749ed6 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -327,4 +327,47 @@ cond_8: { a = condition() ? condition() : b; a = condition() ? condition() : b; } +} + +cond_9: { + options = { + conditionals: true, + evaluate : true + }; + input: { + // compress these + a = condition ? true : false; + + a = !condition ? true : false; + + a = condition() ? true : false; + + if (condition) { + a = true; + } else { + a = false; + } + + a = condition ? false : true; + + a = !condition ? false : true; + + a = condition() ? false : true; + + if (condition) { + a = false; + } else { + a = true; + } + } + expect: { + a = !!condition; + a = !condition; + a = !!condition(); + a = !!condition; + a = !condition; + a = !!condition; + a = !condition(); + a = !condition; + } } \ No newline at end of file From a16d1d1d1244c3ed75369a7f0113ddac6b00abe7 Mon Sep 17 00:00:00 2001 From: Tal Ater Date: Sat, 27 Sep 2014 02:49:10 +0300 Subject: [PATCH 3/3] Reverted previous addition of compression of conditionals where the condition and consequent are equivalent. --- lib/compress.js | 9 --------- test/compress/conditionals.js | 36 ----------------------------------- 2 files changed, 45 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index ac5d0d50..bf2d166e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2332,15 +2332,6 @@ merge(Compressor.prototype, { } } - // x=y?y:z --> x=y||z - if (self.condition.equivalent_to(consequent) - && !self.condition.has_side_effects(compressor)) { - return make_node(AST_Binary, self.condition, { - left: self.condition, - operator: "||", - right: alternative - }); - } // x=y?true:false --> x=!!y if (consequent instanceof AST_True && alternative instanceof AST_False) { diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index ce749ed6..d9c75f18 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -294,42 +294,6 @@ cond_7: { } cond_8: { - options = { - conditionals: true, - evaluate : true - }; - input: { - // compress these - a = condition ? condition : b; - - if (condition) { - a = condition; - } else { - a = b; - } - - a = condition ? condition : b(); - - // Don't compress conditions that have side effects - if (condition()) { - a = condition(); - } else { - a = b; - } - - a = condition() ? condition() : b; - - } - expect: { - a = condition || b; - a = condition || b; - a = condition || b(); - a = condition() ? condition() : b; - a = condition() ? condition() : b; - } -} - -cond_9: { options = { conditionals: true, evaluate : true