}
};
+function benchmark(runner) {
+ return function (processor, action) {
+ var name = processor.constructor.name + '#' + action;
+ var start = process.hrtime();
+ runner(processor, action);
+ var itTook = process.hrtime(start);
+ console.log('%d ms: ' + name, 1000 * itTook[0] + itTook[1] / 1000000);
+ };
+}
+
function minify(data, callback) {
var startedAt;
var stats = this.stats;
var freeTextProcessor = new FreeTextProcessor();
var urlsProcessor = new UrlsProcessor();
- var replace = function() {
- if (typeof arguments[0] == 'function')
- arguments[0]();
- else
- data = data.replace.apply(data, arguments);
- };
+ var urlRebase = new UrlRebase(options, context);
- // replace function
- if (options.benchmark) {
- var originalReplace = replace;
- replace = function(pattern, replacement) {
- var name = typeof pattern == 'function' ?
- /function (\w+)\(/.exec(pattern.toString())[1] :
- pattern;
+ var run = function (processor, action, enabled) {
+ if (enabled === false)
+ return;
- var start = process.hrtime();
- originalReplace(pattern, replacement);
+ data = processor[action](data);
+ };
- var itTook = process.hrtime(start);
- console.log('%d ms: ' + name, 1000 * itTook[0] + itTook[1] / 1000000);
- };
- }
+ if (options.benchmark)
+ run = benchmark(run);
if (options.debug) {
startedAt = process.hrtime();
stats.originalSize = data.length;
}
- replace(function escapeComments() {
- data = commentsProcessor.escape(data);
- });
-
- replace(function escapeExpressions() {
- data = expressionsProcessor.escape(data);
- });
-
- replace(function escapeFreeText() {
- data = freeTextProcessor.escape(data);
- });
-
- replace(function escapeUrls() {
- data = urlsProcessor.escape(data);
- });
-
- replace(function restoreUrls() {
- data = urlsProcessor.restore(data);
- });
-
- replace(function rebaseUrls() {
- data = options.noRebase ? data : new UrlRebase(options, context).process(data);
- });
-
- replace(function restoreFreeText() {
- data = freeTextProcessor.restore(data);
- });
-
- replace(function restoreComments() {
- data = commentsProcessor.restore(data);
- });
-
- replace(function restoreExpressions() {
- data = expressionsProcessor.restore(data);
- });
+ run(commentsProcessor, 'escape');
+ run(expressionsProcessor, 'escape');
+ run(urlsProcessor, 'escape');
+ run(freeTextProcessor, 'escape');
+ run(selectorsOptimizer, 'process');
+ run(freeTextProcessor, 'restore');
+ run(urlsProcessor, 'restore');
+ run(urlRebase, 'process', !options.noRebase);
+ run(expressionsProcessor, 'restore');
+ run(commentsProcessor, 'restore');
if (options.debug) {
var elapsed = process.hrtime(startedAt);