From 827a8356545e285cd3cbafeb85ea44e909e135d5 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Sun, 4 Sep 2016 21:45:01 -0400 Subject: [PATCH 1/9] Improve benchmarks accuracy --- package.json | 18 ++++- .../__tests__/simplify-test.js | 4 +- .../babel-plugin-minify-simplify/src/index.js | 7 +- scripts/benchmark.js | 77 ++++++++++++++++--- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 253f7525d..a7aae102c 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,22 @@ "lerna": "2.0.0-beta.26", "lerna-changelog": "^0.2.1", "through2": "^2.0.1", - "uglify-js": "^2.7.3" + "uglify-js": "^2.7.3", + + "babel-plugin-minify-constant-folding": "^0.0.1", + "babel-plugin-minify-dead-code-elimination": "^0.0.1", + "babel-plugin-minify-flip-comparisons": "^0.0.1", + "babel-plugin-minify-guarded-expressions": "^0.0.1", + "babel-plugin-minify-infinity": "^0.0.1", + "babel-plugin-minify-mangle-names": "^0.0.2", + "babel-plugin-minify-replace": "^0.0.1", + "babel-plugin-minify-simplify": "^0.0.2", + "babel-plugin-minify-type-constructors": "^0.0.1", + "babel-plugin-transform-member-expression-literals": "^6.8.0", + "babel-plugin-transform-merge-sibling-variables": "^6.8.0", + "babel-plugin-transform-minify-booleans": "^6.8.0", + "babel-plugin-transform-property-literals": "^6.8.0", + "babel-plugin-transform-simplify-comparison-operators": "^6.8.0", + "babel-plugin-transform-undefined-to-void": "^6.8.0" } } diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 686a572f3..4ea134c7b 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -6,7 +6,9 @@ const unpad = require("../../../utils/unpad"); function transform(code) { return babel.transform(code, { - plugins: [plugin], + plugins: [ + [plugin, { multiPass: true }], + ], }).code; } diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index 61284c9bb..b34a5574b 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -333,7 +333,12 @@ module.exports = ({ types: t }) => { Function: { enter: earlyReturnTransform, - exit(path) { + exit(path, state) { + const multiPass = state.opts.multiPass === false ? false : true; + if (!multiPass) { + return; + } + // Useful to do on enter and exit because more oppurtinties can open. earlyReturnTransform(path); diff --git a/scripts/benchmark.js b/scripts/benchmark.js index c72346d42..386ae44c7 100755 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -14,6 +14,8 @@ const fs = require("fs"); const path = require("path"); const compile = require("google-closure-compiler-js").compile; +const NUM_TEST_RUNS = 10; + const filename = process.argv[2]; if (!filename) { console.error("Error: No filename specified"); @@ -21,7 +23,7 @@ if (!filename) { } const table = new Table({ - head: ["", "raw", "raw win", "gzip", "gzip win", "parse time", "run"], + head: ["", "raw", "raw win", "gzip", "gzip win", "parse time", "run time (average)"], chars: { top: "", "top-mid": "" , @@ -52,7 +54,8 @@ const code = fs.readFileSync(filename, "utf8"); const gzippedCode = zlib.gzipSync(code); function test(name, callback) { - console.log("testing", name); + + console.log('testing', name); const start = Date.now(); const result = callback(code); @@ -64,24 +67,82 @@ function test(name, callback) { const gzipped = zlib.gzipSync(result); const parseStart = Date.now(); - // disable for now we have a failing test in dce new Function(result); const parseEnd = Date.now(); const parseNow = parseEnd - parseStart; + const runTimes = [run]; + + for (var i = 1; i < NUM_TEST_RUNS; i++) { + const start = Date.now(); + const result = callback(code); + runTimes.push(Date.now() - start); + } + + const totalTime = runTimes.reduce((a, b) => a + b, 0); + const average = parseInt(totalTime / runTimes.length, 10); + results.push({ name: name, raw: result.length, gzip: gzipped.length, parse: parseNow, - run: run, + run: average, }); } -test("babili", function (code) { +test("babili (best size)", function (code) { return babel.transform(code, { sourceType: "script", - presets: [require("../packages/babel-preset-babili")], + minified: true, + plugins: [ + require("babel-plugin-minify-constant-folding"), + require("babel-plugin-minify-dead-code-elimination"), + require("babel-plugin-minify-flip-comparisons"), + require("babel-plugin-minify-guarded-expressions"), + require("babel-plugin-minify-infinity"), + require("babel-plugin-minify-mangle-names"), + require("babel-plugin-minify-replace"), + [ + require("../packages/babel-plugin-minify-simplify/lib/index.js"), + { multiPass: true } + ], + require("babel-plugin-minify-type-constructors"), + require("babel-plugin-transform-member-expression-literals"), + require("babel-plugin-transform-merge-sibling-variables"), + require("babel-plugin-transform-minify-booleans"), + require("babel-plugin-transform-property-literals"), + require("babel-plugin-transform-simplify-comparison-operators"), + require("babel-plugin-transform-undefined-to-void"), + ], + comments: false, + }).code; +}); + +test("babili (best speed)", function (code) { + return babel.transform(code, { + sourceType: "script", + minified: true, + plugins: [ + require("babel-plugin-minify-constant-folding"), + require("babel-plugin-minify-dead-code-elimination"), + require("babel-plugin-minify-flip-comparisons"), + require("babel-plugin-minify-guarded-expressions"), + require("babel-plugin-minify-infinity"), + require("babel-plugin-minify-mangle-names"), + require("babel-plugin-minify-replace"), + [ + require("../packages/babel-plugin-minify-simplify/lib/index.js"), + { multiPass: false } + ], + require("babel-plugin-minify-type-constructors"), + require("babel-plugin-transform-member-expression-literals"), + require("babel-plugin-transform-merge-sibling-variables"), + require("babel-plugin-transform-minify-booleans"), + require("babel-plugin-transform-property-literals"), + require("babel-plugin-transform-simplify-comparison-operators"), + require("babel-plugin-transform-undefined-to-void"), + ], comments: false, }).code; }); @@ -107,9 +168,7 @@ test("uglify", function (code) { }).code; }); -results = results.sort(function (a, b) { - return a.gzip > b.gzip; -}); +results = results.sort((a, b) => a.gzip > b.gzip); results.forEach(function (result, i) { let row = [ From 4fa81c5af97caaa499abf63c438865b1df6b7dfb Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Sun, 4 Sep 2016 21:47:12 -0400 Subject: [PATCH 2/9] Fix test --- .../babel-plugin-minify-simplify/__tests__/simplify-test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 4ea134c7b..686a572f3 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -6,9 +6,7 @@ const unpad = require("../../../utils/unpad"); function transform(code) { return babel.transform(code, { - plugins: [ - [plugin, { multiPass: true }], - ], + plugins: [plugin], }).code; } From aee567a4f316f70af408f3f67f104841c78730b3 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 25 Nov 2016 17:19:27 -0500 Subject: [PATCH 3/9] Remove plugins from package.json --- package.json | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/package.json b/package.json index ba60f9b60..c47e7bd57 100644 --- a/package.json +++ b/package.json @@ -48,22 +48,6 @@ "lerna": "2.0.0-beta.26", "lerna-changelog": "^0.2.1", "through2": "^2.0.1", - "uglify-js": "^2.7.3", - - "babel-plugin-minify-constant-folding": "^0.0.1", - "babel-plugin-minify-dead-code-elimination": "^0.0.1", - "babel-plugin-minify-flip-comparisons": "^0.0.1", - "babel-plugin-minify-guarded-expressions": "^0.0.1", - "babel-plugin-minify-infinity": "^0.0.1", - "babel-plugin-minify-mangle-names": "^0.0.2", - "babel-plugin-minify-replace": "^0.0.1", - "babel-plugin-minify-simplify": "^0.0.2", - "babel-plugin-minify-type-constructors": "^0.0.1", - "babel-plugin-transform-member-expression-literals": "^6.8.0", - "babel-plugin-transform-merge-sibling-variables": "^6.8.0", - "babel-plugin-transform-minify-booleans": "^6.8.0", - "babel-plugin-transform-property-literals": "^6.8.0", - "babel-plugin-transform-simplify-comparison-operators": "^6.8.0", - "babel-plugin-transform-undefined-to-void": "^6.8.0" + "uglify-js": "^2.7.3" } } From a8c3740a76954b2f604944986e2752fc5b839b57 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 25 Nov 2016 17:24:08 -0500 Subject: [PATCH 4/9] Revert parts of benchamrk --- scripts/benchmark.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/scripts/benchmark.js b/scripts/benchmark.js index 5ee79a68a..debfc44ba 100755 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -19,10 +19,6 @@ let packagename, filename; const NUM_TEST_RUNS = 10; -const filename = process.argv[2]; -if (!filename) { - console.error("Error: No filename specified"); - const script = new Command("benchmark.js") .option("-o, --offline", "Only install package if not present; package not removed after testing") .usage("[options] [file]") @@ -171,8 +167,41 @@ function testFile() { return out.compiledCode; }); + test("uglify", function (code) { + return uglify.minify(code, { + fromString: true, + }).code; + }); +} + +function processResults() { results = results.sort((a, b) => a.gzip > b.gzip); + results.forEach(function (result, i) { + let row = [ + chalk.bold(result.name), + bytes(result.raw), + Math.round(((code.length / result.raw) * 100) - 100) + "%", + bytes(result.gzip), + Math.round(((gzippedCode.length / result.gzip) * 100) - 100) + "%", + Math.round(result.parse) + "ms", + Math.round(result.run) + "ms", + ]; + + let style = chalk.yellow; + if (i === 0) { + style = chalk.green; + } + if (i === results.length - 1) { + style = chalk.red; + } + row = row.map(function (item) { + return style(item); + }); + + table.push(row); + }); + console.log(table.toString()); } From 0449171ac0db3e3ed3d5d77a2a490582a6578a54 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 25 Nov 2016 17:38:59 -0500 Subject: [PATCH 5/9] Add best size / best speed --- scripts/benchmark.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/scripts/benchmark.js b/scripts/benchmark.js index debfc44ba..c65495e5c 100755 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -17,7 +17,7 @@ const compile = require("google-closure-compiler-js").compile; let packagename, filename; -const NUM_TEST_RUNS = 10; +const NUM_TEST_RUNS = 3; const script = new Command("benchmark.js") .option("-o, --offline", "Only install package if not present; package not removed after testing") @@ -143,10 +143,30 @@ function testFile() { code = fs.readFileSync(filename, "utf8"); gzippedCode = zlib.gzipSync(code); - test("babili", function (code) { + test("babili (best speed)", function (code) { return babel.transform(code, { sourceType: "script", - presets: [require("../packages/babel-preset-babili")], + presets: [ + [require("../packages/babel-preset-babili"), { + simplify: { + multiPass: false + } + }] + ], + comments: false, + }).code; + }); + + test("babili (best size)", function (code) { + return babel.transform(code, { + sourceType: "script", + presets: [ + [require("../packages/babel-preset-babili"), { + simplify: { + multiPass: true + } + }] + ], comments: false, }).code; }); From 069ba3425ebf0980465d88f6142ba1a2a87d7b5e Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 25 Nov 2016 18:24:47 -0500 Subject: [PATCH 6/9] Add an option to simplify --- packages/babel-plugin-minify-simplify/src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index d8d0e2d9f..2c62bf142 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -507,7 +507,12 @@ module.exports = ({ types: t }) => { }, Function: { - exit(path) { + exit(path, state) { + const multiPass = state.opts.multiPass === false ? false : true; + if (!multiPass) { + return; + } + earlyReturnTransform(path); if (!path.node[shouldRevisit]) { From 4c4c10f42c8071c8b9ade0b92bb31b566008e204 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 25 Nov 2016 18:48:19 -0500 Subject: [PATCH 7/9] Fix linter --- scripts/benchmark.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/benchmark.js b/scripts/benchmark.js index c65495e5c..d0b3fa70f 100755 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -103,7 +103,7 @@ function checkFile() { function test(name, callback) { - console.log('testing', name); + console.log("testing", name); const start = Date.now(); const result = callback(code); @@ -121,9 +121,9 @@ function test(name, callback) { const runTimes = [run]; - for (var i = 1; i < NUM_TEST_RUNS; i++) { + for (let i = 1; i < NUM_TEST_RUNS; i++) { const start = Date.now(); - const result = callback(code); + callback(code); runTimes.push(Date.now() - start); } From d051c44c887c6d5ce48efa3c0ef948dc1d81a0fb Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Sun, 27 Nov 2016 16:17:48 -0500 Subject: [PATCH 8/9] Remove multiPass --- .../babel-plugin-minify-simplify/src/index.js | 5 ----- scripts/benchmark.js | 16 ++-------------- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index 2c62bf142..a9f7b2405 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -508,11 +508,6 @@ module.exports = ({ types: t }) => { Function: { exit(path, state) { - const multiPass = state.opts.multiPass === false ? false : true; - if (!multiPass) { - return; - } - earlyReturnTransform(path); if (!path.node[shouldRevisit]) { diff --git a/scripts/benchmark.js b/scripts/benchmark.js index d0b3fa70f..69cf507dd 100755 --- a/scripts/benchmark.js +++ b/scripts/benchmark.js @@ -146,13 +146,7 @@ function testFile() { test("babili (best speed)", function (code) { return babel.transform(code, { sourceType: "script", - presets: [ - [require("../packages/babel-preset-babili"), { - simplify: { - multiPass: false - } - }] - ], + presets: [require("../packages/babel-preset-babili")], comments: false, }).code; }); @@ -160,13 +154,7 @@ function testFile() { test("babili (best size)", function (code) { return babel.transform(code, { sourceType: "script", - presets: [ - [require("../packages/babel-preset-babili"), { - simplify: { - multiPass: true - } - }] - ], + presets: [require("../packages/babel-preset-babili")], comments: false, }).code; }); From 4ee58604b9a0d75a107ad2036d73e05443b050a5 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Sun, 27 Nov 2016 16:19:43 -0500 Subject: [PATCH 9/9] Remove state arg --- packages/babel-plugin-minify-simplify/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-simplify/src/index.js b/packages/babel-plugin-minify-simplify/src/index.js index a9f7b2405..d8d0e2d9f 100644 --- a/packages/babel-plugin-minify-simplify/src/index.js +++ b/packages/babel-plugin-minify-simplify/src/index.js @@ -507,7 +507,7 @@ module.exports = ({ types: t }) => { }, Function: { - exit(path, state) { + exit(path) { earlyReturnTransform(path); if (!path.node[shouldRevisit]) {