Speeds up precision minification.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 5 Oct 2014 14:39:38 +0000 (15:39 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:45 +0000 (21:22 +0100)
* Caches minification regexp.
* Skips minification if no decimal dots in value.

lib/selectors/optimizers/simple.js

index cf0f2a3..a112966 100644 (file)
@@ -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);