From e90e768e412baf7887a735e04a0139a262927f59 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 5 Oct 2014 15:39:38 +0100 Subject: [PATCH] Speeds up precision minification. * Caches minification regexp. * Skips minification if no decimal dots in value. --- lib/selectors/optimizers/simple.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index cf0f2a3c..a112966e 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -18,6 +18,13 @@ function SimpleOptimizer(options, context) { if (['ie7', 'ie8'].indexOf(options.compatibility) == -1) units.push('rem'); options.unitsRegexp = new RegExp('(^|\\s|\\(|,)0(?:' + units.join('|') + ')', 'g'); + + options.precision = {}; + options.precision.value = options.roundingPrecision === undefined ? + DEFAULT_ROUNDING_PRECISION : + options.roundingPrecision; + options.precision.multiplier = Math.pow(10, options.precision.value); + options.precision.regexp = new RegExp('\\.(\\d{' + (options.precision.value + 1) + ',})px', 'g'); } function removeUnsupported(token, compatibility) { @@ -94,15 +101,14 @@ function zeroMinifier(_, value) { .replace(/(^|\D)0\.(\d)/g, '$1.$2'); } -function precisionMinifier(_, value, precision) { - if (precision === undefined) - precision = DEFAULT_ROUNDING_PRECISION; - var decimalMultiplier = Math.pow(10, precision); +function precisionMinifier(_, value, precisionOptions) { + if (value.indexOf('.') === -1) + return value; return value - .replace(new RegExp('\\.(\\d{' + (precision + 1) + ',})px', 'g'), function(match, decimalPlaces) { - var newFraction = Math.round(parseFloat('.' + decimalPlaces) * decimalMultiplier) / decimalMultiplier; - return precision === 0 || newFraction === 0 ? + .replace(precisionOptions.regexp, function(match, decimalPlaces) { + var newFraction = Math.round(parseFloat('.' + decimalPlaces) * precisionOptions.multiplier) / precisionOptions.multiplier; + return precisionOptions.value === 0 || newFraction === 0 ? 'px' : '.' + ('' + newFraction).substring('0.'.length) + 'px'; }) @@ -184,7 +190,7 @@ function reduce(body, options) { value = valueMinifiers[property](value); value = zeroMinifier(property, value); - value = precisionMinifier(property, value, options.roundingPrecision); + value = precisionMinifier(property, value, options.precision); value = unitMinifier(property, value, options.unitsRegexp); value = multipleZerosMinifier(property, value); value = colorMininifier(property, value, options.compatibility); -- 2.34.1