From fedb6191a11c2d868c23362e10f23795aae3ca3e Mon Sep 17 00:00:00 2001 From: kzc Date: Thu, 11 Jun 2015 23:22:38 -0400 Subject: optimizations for && and || where left side is constant expression --- lib/compress.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lib/compress.js') diff --git a/lib/compress.js b/lib/compress.js index 530e7c2f..34233143 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2104,6 +2104,34 @@ merge(Compressor.prototype, { } break; } + if (compressor.option("conditionals")) { + if (self.operator == "&&") { + var ll = self.left.evaluate(compressor); + var rr = self.right.evaluate(compressor); + if (ll.length > 1) { + if (ll[1]) { + compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start); + return rr[0]; + } else { + compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start); + return ll[0]; + } + } + } + if (self.operator == "||") { + var ll = self.left.evaluate(compressor); + var rr = self.right.evaluate(compressor); + if (ll.length > 1) { + if (ll[1]) { + compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start); + return ll[0]; + } else { + compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start); + return rr[0]; + } + } + } + } if (compressor.option("booleans") && compressor.in_boolean_context()) switch (self.operator) { case "&&": var ll = self.left.evaluate(compressor); -- cgit v1.2.3 From f47b2b52a5642cf7991329a2b635f8f1168dc8c9 Mon Sep 17 00:00:00 2001 From: kzc Date: Sun, 14 Jun 2015 17:44:26 -0400 Subject: operator && and || optimization: add "else" before "if" as intended --- lib/compress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/compress.js') diff --git a/lib/compress.js b/lib/compress.js index 34233143..343edc46 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2118,7 +2118,7 @@ merge(Compressor.prototype, { } } } - if (self.operator == "||") { + else if (self.operator == "||") { var ll = self.left.evaluate(compressor); var rr = self.right.evaluate(compressor); if (ll.length > 1) { -- cgit v1.2.3