From ef0f228db5be5f1419de915e435258b11a833640 Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 28 Oct 2015 13:25:27 -0400 Subject: [PATCH 1/4] optimize `return undefined` and `return void 0` --- lib/compress.js | 10 ++ test/compress/return_undefined.js | 229 ++++++++++++++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 test/compress/return_undefined.js diff --git a/lib/compress.js b/lib/compress.js index 216aade9..ebe7e957 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -65,6 +65,7 @@ function Compressor(options, false_by_default) { keep_fnames : false, hoist_vars : false, if_return : !false_by_default, + return_void_0 : !false_by_default, join_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, @@ -2519,4 +2520,13 @@ merge(Compressor.prototype, { OPT(AST_Object, literals_in_boolean_context); OPT(AST_RegExp, literals_in_boolean_context); + OPT(AST_Return, function(self, compressor){ + if (compressor.option("return_void_0")) { + if (self.value instanceof AST_Undefined) { + self.value = null; + } + } + return self; + }); + })(); diff --git a/test/compress/return_undefined.js b/test/compress/return_undefined.js new file mode 100644 index 00000000..be79d89d --- /dev/null +++ b/test/compress/return_undefined.js @@ -0,0 +1,229 @@ +return_void_0_true: { + options = { + return_void_0 : true, + sequences : false, + if_return : true, + evaluate : true, + dead_code : true, + conditionals : true, + comparisons : true, + booleans : true, + unused : true, + side_effects : true, + properties : true, + drop_debugger : true, + loops : true, + hoist_funs : true, + keep_fargs : true, + keep_fnames : false, + hoist_vars : true, + join_vars : true, + cascade : true, + negate_iife : true + }; + input: { + function f0() { + } + function f1() { + return undefined; + } + function f2() { + return void 0; + } + function f3() { + return void 123; + } + function f4() { + return; + } + function f5(a, b) { + console.log(a, b); + baz(a); + return; + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return undefined; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return undefined; + } + } + expect: { + function f0() {} + function f1() {} + function f2() {} + function f3() {} + function f4() {} + function f5(a, b) { + console.log(a, b); + baz(a); + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + } + function f7(a, b) { + console.log(a, b); + if (!a) + return a + b; + foo(b); + baz(a); + } + function f8(a, b) { + foo(a); + bar(b); + } + function f9(a, b) { + foo(a); + bar(b); + } + } +} + +return_void_0_false: { + options = { + return_void_0 : false, + sequences : false, + if_return : true, + evaluate : true, + dead_code : true, + conditionals : true, + comparisons : true, + booleans : true, + unused : true, + side_effects : true, + properties : true, + drop_debugger : true, + loops : true, + hoist_funs : true, + keep_fargs : true, + keep_fnames : false, + hoist_vars : true, + join_vars : true, + cascade : true, + negate_iife : true + }; + input: { + function f0() { + } + function f1() { + return undefined; + } + function f2() { + return void 0; + } + function f3() { + return void 123; + } + function f4() { + return; + } + function f5(a, b) { + console.log(a, b); + baz(a); + return; + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return undefined; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return undefined; + } + } + expect: { + function f0() { + } + function f1() { + return void 0; + } + function f2() { + return void 0; + } + function f3() { + return void 0; + } + function f4() { + } + function f5(a, b) { + console.log(a, b); + baz(a); + } + function f6(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return a + b; + } + return void 0; + } + function f7(a, b) { + console.log(a, b); + if (a) { + foo(b); + baz(a); + return void 0; + } + return a + b; + } + function f8(a, b) { + foo(a); + bar(b); + return void 0; + } + function f9(a, b) { + foo(a); + bar(b); + return void 0; + } + } +} + From 1ad0630fc93e0b38854a3def4e3e9966f26d447f Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 28 Oct 2015 13:41:41 -0400 Subject: [PATCH 2/4] more tests for `return undefined` optimization --- test/compress/return_undefined.js | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/compress/return_undefined.js b/test/compress/return_undefined.js index be79d89d..b1195a72 100644 --- a/test/compress/return_undefined.js +++ b/test/compress/return_undefined.js @@ -69,6 +69,15 @@ return_void_0_true: { bar(b); return undefined; } + function f10() { + return false; + } + function f11() { + return null; + } + function f12() { + return 0; + } } expect: { function f0() {} @@ -103,6 +112,15 @@ return_void_0_true: { foo(a); bar(b); } + function f10() { + return !1; + } + function f11() { + return null; + } + function f12() { + return 0; + } } } @@ -177,6 +195,15 @@ return_void_0_false: { bar(b); return undefined; } + function f10() { + return false; + } + function f11() { + return null; + } + function f12() { + return 0; + } } expect: { function f0() { @@ -224,6 +251,15 @@ return_void_0_false: { bar(b); return void 0; } + function f10() { + return !1; + } + function f11() { + return null; + } + function f12() { + return 0; + } } } From c93bcffb3ef71e5032a1e303d691620541670425 Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 28 Oct 2015 13:55:23 -0400 Subject: [PATCH 3/4] drop iojs testing in .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0324385f..0f65db5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: node_js before_install: "npm install -g npm" node_js: - - "iojs" - "0.12" - "0.10" - "4" From a26529df280a7796116606368421caf252d4c557 Mon Sep 17 00:00:00 2001 From: kzc Date: Wed, 28 Oct 2015 17:51:46 -0400 Subject: [PATCH 4/4] `return undefined` optimization no longer uses `return_void_0` option --- lib/compress.js | 7 +- test/compress/return_undefined.js | 143 +----------------------------- 2 files changed, 3 insertions(+), 147 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index ebe7e957..50353fe0 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -65,7 +65,6 @@ function Compressor(options, false_by_default) { keep_fnames : false, hoist_vars : false, if_return : !false_by_default, - return_void_0 : !false_by_default, join_vars : !false_by_default, cascade : !false_by_default, side_effects : !false_by_default, @@ -2521,10 +2520,8 @@ merge(Compressor.prototype, { OPT(AST_RegExp, literals_in_boolean_context); OPT(AST_Return, function(self, compressor){ - if (compressor.option("return_void_0")) { - if (self.value instanceof AST_Undefined) { - self.value = null; - } + if (self.value instanceof AST_Undefined) { + self.value = null; } return self; }); diff --git a/test/compress/return_undefined.js b/test/compress/return_undefined.js index b1195a72..9662aa51 100644 --- a/test/compress/return_undefined.js +++ b/test/compress/return_undefined.js @@ -1,6 +1,5 @@ -return_void_0_true: { +return_undefined: { options = { - return_void_0 : true, sequences : false, if_return : true, evaluate : true, @@ -123,143 +122,3 @@ return_void_0_true: { } } } - -return_void_0_false: { - options = { - return_void_0 : false, - sequences : false, - if_return : true, - evaluate : true, - dead_code : true, - conditionals : true, - comparisons : true, - booleans : true, - unused : true, - side_effects : true, - properties : true, - drop_debugger : true, - loops : true, - hoist_funs : true, - keep_fargs : true, - keep_fnames : false, - hoist_vars : true, - join_vars : true, - cascade : true, - negate_iife : true - }; - input: { - function f0() { - } - function f1() { - return undefined; - } - function f2() { - return void 0; - } - function f3() { - return void 123; - } - function f4() { - return; - } - function f5(a, b) { - console.log(a, b); - baz(a); - return; - } - function f6(a, b) { - console.log(a, b); - if (a) { - foo(b); - baz(a); - return a + b; - } - return undefined; - } - function f7(a, b) { - console.log(a, b); - if (a) { - foo(b); - baz(a); - return void 0; - } - return a + b; - } - function f8(a, b) { - foo(a); - bar(b); - return void 0; - } - function f9(a, b) { - foo(a); - bar(b); - return undefined; - } - function f10() { - return false; - } - function f11() { - return null; - } - function f12() { - return 0; - } - } - expect: { - function f0() { - } - function f1() { - return void 0; - } - function f2() { - return void 0; - } - function f3() { - return void 0; - } - function f4() { - } - function f5(a, b) { - console.log(a, b); - baz(a); - } - function f6(a, b) { - console.log(a, b); - if (a) { - foo(b); - baz(a); - return a + b; - } - return void 0; - } - function f7(a, b) { - console.log(a, b); - if (a) { - foo(b); - baz(a); - return void 0; - } - return a + b; - } - function f8(a, b) { - foo(a); - bar(b); - return void 0; - } - function f9(a, b) { - foo(a); - bar(b); - return void 0; - } - function f10() { - return !1; - } - function f11() { - return null; - } - function f12() { - return 0; - } - } -} -