[Pkg-javascript-commits] [uglifyjs] 150/228: optimize conditional when condition symbol matches consequent (#1684)

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:25 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 5509e51098274b28b8574246011767ba0be66edd
Author: kzc <kzc at users.noreply.github.com>
Date:   Sun Mar 26 04:36:33 2017 -0400

    optimize conditional when condition symbol matches consequent (#1684)
---
 lib/compress.js               | 11 +++++++++++
 test/compress/conditionals.js | 29 +++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/lib/compress.js b/lib/compress.js
index 8d1ef8a..70bbb7f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3565,8 +3565,19 @@ merge(Compressor.prototype, {
                 alternative: self.consequent
             });
         }
+        var condition = self.condition;
         var consequent = self.consequent;
         var alternative = self.alternative;
+        // x?x:y --> x||y
+        if (condition instanceof AST_SymbolRef
+            && consequent instanceof AST_SymbolRef
+            && condition.definition() === consequent.definition()) {
+            return make_node(AST_Binary, self, {
+                operator: "||",
+                left: condition,
+                right: alternative
+            });
+        }
         // if (foo) exp = something; else exp = something_else;
         //                   |
         //                   v
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index c563983..e7ea2bb 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -933,3 +933,32 @@ issue_1645_2: {
     }
     expect_stdout: true
 }
+
+condition_symbol_matches_consequent: {
+    options = {
+        conditionals: true,
+    }
+    input: {
+        function foo(x, y) {
+            return x ? x : y;
+        }
+        function bar() {
+            return g ? g : h;
+        }
+        var g = 4;
+        var h = 5;
+        console.log(foo(3, null), foo(0, 7), foo(true, false), bar());
+    }
+    expect: {
+        function foo(x, y) {
+            return x || y;
+        }
+        function bar() {
+            return g || h;
+        }
+        var g = 4;
+        var h = 5;
+        console.log(foo(3, null), foo(0, 7), foo(true, false), bar());
+    }
+    expect_stdout: "3 7 true 4"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list