From 793d61499b4ab53cdfdd3b81b9faf9f36b77bae8 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 25 May 2017 07:15:55 +0800 Subject: report timing breakdown (#2000) fix corner cases with `sourceMap` fixes #1998 --- lib/minify.js | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/minify.js b/lib/minify.js index a827930a..16f5b189 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -30,9 +30,6 @@ function set_shorthand(name, options, keys) { function minify(files, options) { var warn_function = AST_Node.warn_function; try { - if (typeof files == "string") { - files = [ files ]; - } options = defaults(options, { compress: {}, ie8: false, @@ -41,10 +38,14 @@ function minify(files, options) { output: {}, parse: {}, sourceMap: false, + timings: false, toplevel: false, warnings: false, wrap: false, }, true); + var timings = options.timings && { + start: Date.now() + }; set_shorthand("ie8", options, [ "compress", "mangle", "output" ]); set_shorthand("keep_fnames", options, [ "compress", "mangle" ]); set_shorthand("toplevel", options, [ "compress", "mangle" ]); @@ -75,10 +76,14 @@ function minify(files, options) { warnings.push(warning); }; } + if (timings) timings.parse = Date.now(); var toplevel; if (files instanceof AST_Toplevel) { toplevel = files; } else { + if (typeof files == "string") { + files = [ files ]; + } options.parse = options.parse || {}; options.parse.toplevel = null; for (var name in files) { @@ -95,19 +100,23 @@ function minify(files, options) { if (options.wrap) { toplevel = toplevel.wrap_commonjs(options.wrap); } - if (options.compress) { - toplevel.figure_out_scope(options.mangle); - toplevel = new Compressor(options.compress).compress(toplevel); - } + if (timings) timings.scope1 = Date.now(); + if (options.compress) toplevel.figure_out_scope(options.mangle); + if (timings) timings.compress = Date.now(); + if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel); + if (timings) timings.scope2 = Date.now(); + if (options.mangle) toplevel.figure_out_scope(options.mangle); + if (timings) timings.mangle = Date.now(); if (options.mangle) { - toplevel.figure_out_scope(options.mangle); base54.reset(); toplevel.compute_char_frequency(options.mangle); toplevel.mangle_names(options.mangle); - if (options.mangle.properties) { - toplevel = mangle_properties(toplevel, options.mangle.properties); - } } + if (timings) timings.properties = Date.now(); + if (options.mangle && options.mangle.properties) { + toplevel = mangle_properties(toplevel, options.mangle.properties); + } + if (timings) timings.output = Date.now(); var result = {}; if (options.output.ast) { result.ast = toplevel; @@ -123,7 +132,9 @@ function minify(files, options) { root: options.sourceMap.root }); if (options.sourceMap.includeSources) { - for (var name in files) { + if (files instanceof AST_Toplevel) { + throw new Error("original source content unavailable"); + } else for (var name in files) { options.output.source_map.get().setSourceContent(name, files[name]); } } @@ -142,6 +153,18 @@ function minify(files, options) { } } } + if (timings) { + timings.end = Date.now(); + result.timings = { + parse: 1e-3 * (timings.scope1 - timings.parse), + scope: 1e-3 * (timings.compress - timings.scope1 + timings.mangle - timings.scope2), + compress: 1e-3 * (timings.scope2 - timings.compress), + mangle: 1e-3 * (timings.properties - timings.mangle), + properties: 1e-3 * (timings.output - timings.properties), + output: 1e-3 * (timings.end - timings.output), + total: 1e-3 * (timings.end - timings.start) + } + } if (warnings.length) { result.warnings = warnings; } -- cgit v1.2.3