From af34af5706733e2a367498c404759c0f290c9f2d Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 5 Oct 2014 15:16:50 +0100 Subject: [PATCH] Speeds up unit minification by caching regexp. --- lib/selectors/optimizers/simple.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index 2ce82112..cf0f2a3c 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -13,6 +13,11 @@ var CHARSET_REGEXP = new RegExp('^' + CHARSET_TOKEN, 'i'); function SimpleOptimizer(options, context) { this.options = options; this.propertyOptimizer = new PropertyOptimizer(this.options.compatibility, this.options.aggressiveMerging, context); + + var units = ['px', 'em', 'ex', 'cm', 'mm', 'in', 'pt', 'pc', '%']; + if (['ie7', 'ie8'].indexOf(options.compatibility) == -1) + units.push('rem'); + options.unitsRegexp = new RegExp('(^|\\s|\\(|,)0(?:' + units.join('|') + ')', 'g'); } function removeUnsupported(token, compatibility) { @@ -104,12 +109,8 @@ function precisionMinifier(_, value, precision) { .replace(/(\d)\.($|\D)/g, '$1$2'); } -function unitMinifier(_, value, compatibility) { - var units = ['px', 'em', 'ex', 'cm', 'mm', 'in', 'pt', 'pc', '%']; - if (['ie7', 'ie8'].indexOf(compatibility) == -1) - units.push('rem'); - - return value.replace(new RegExp('(^|\\s|\\(|,)0(?:' + units.join('|') + ')', 'g'), '$1' + '0'); +function unitMinifier(_, value, unitsRegexp) { + return value.replace(unitsRegexp, '$1' + '0'); } function multipleZerosMinifier(property, value) { @@ -184,7 +185,7 @@ function reduce(body, options) { value = zeroMinifier(property, value); value = precisionMinifier(property, value, options.roundingPrecision); - value = unitMinifier(property, value, options.compatibility); + value = unitMinifier(property, value, options.unitsRegexp); value = multipleZerosMinifier(property, value); value = colorMininifier(property, value, options.compatibility); -- 2.34.1