From: Jakub Pawlowicz Date: Sat, 14 Jan 2017 10:30:04 +0000 (+0100) Subject: See #799 - speeds up serializing code. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=bb1e8ffe1e1ba00801abee66da4a2a440fb8909c;p=clean-css.git See #799 - speeds up serializing code. Why: * Instead of using an `if` branch implements wrapping and tracking as a callback with `no-op` implementation when wrapping is disabled. --- diff --git a/lib/writer/simple.js b/lib/writer/simple.js index ceecd8ae..5bb16d12 100644 --- a/lib/writer/simple.js +++ b/lib/writer/simple.js @@ -7,13 +7,16 @@ function store(token, serializeContext) { token : token[1]; - if (serializeContext.format.wrapAt && serializeContext.column + value.length > serializeContext.format.wrapAt) { + serializeContext.wrap(value); + serializeContext.track(value); + serializeContext.output.push(value); +} + +function wrap(value, serializeContext) { + if (serializeContext.column + value.length > serializeContext.format.wrapAt) { track(lineBreak, serializeContext); serializeContext.output.push(lineBreak); } - - track(value, serializeContext); - serializeContext.output.push(value); } function track(value, serializeContext) { @@ -32,7 +35,13 @@ function serializeStyles(tokens, context) { line: 1, output: [], spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace, - store: store + store: store, + track: context.options.format.wrapAt ? + function (value) { track(value, serializeContext); } : + function () { /* noop */ }, + wrap: context.options.format.wrapAt ? + function (value) { wrap(value, serializeContext); } : + function () { /* noop */ } }; all(tokens, serializeContext, false); diff --git a/lib/writer/source-maps.js b/lib/writer/source-maps.js index c69dc1a4..f58db4f6 100644 --- a/lib/writer/source-maps.js +++ b/lib/writer/source-maps.js @@ -15,13 +15,16 @@ function store(element, serializeContext) { var value = fromString ? element : element[1]; var mappings = fromString ? null : element[2]; - if (serializeContext.format.wrapAt && serializeContext.column + value.length > serializeContext.format.wrapAt) { + serializeContext.wrap(value); + track(value, mappings, serializeContext); + serializeContext.output.push(value); +} + +function wrap(value, serializeContext) { + if (serializeContext.column + value.length > serializeContext.format.wrapAt) { track(lineBreak, false, serializeContext); serializeContext.output.push(lineBreak); } - - track(value, mappings, serializeContext); - serializeContext.output.push(value); } function track(value, mappings, serializeContext) { @@ -81,7 +84,10 @@ function serializeStylesAndSourceMap(tokens, context) { outputMap: new SourceMapGenerator(), sourcesContent: context.sourcesContent, spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace, - store: store + store: store, + wrap: context.options.format.wrapAt ? + function (value) { wrap(value, serializeContext); } : + function () { /* noop */ } }; all(tokens, serializeContext, false);