From fd089dfc7a6769693483dbf177c062cc19c225a0 Mon Sep 17 00:00:00 2001 From: hika Date: Fri, 18 Aug 2017 14:26:24 +0900 Subject: [PATCH] refector(rateFn): RegularExpression optimize The internal functions toParsedFloat and getConverted are heavy logic used in the for loop. Changes to scope references without generating regular expressions each time, and fixes and optimizes errors in regular expressions. --- src/transform.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/transform.js b/src/transform.js index be11ecf..93be369 100644 --- a/src/transform.js +++ b/src/transform.js @@ -54,7 +54,7 @@ function unit(name) { */ function getConverted(val, base) { let ret = val; - const num = val.match(/((-|\+)*[0-9]+)%/); + const num = val.match(getConvertedRegNum); if (num && num.length >= 1) { ret = `${base * (parseFloat(num[1]) / 100)}px`; @@ -64,6 +64,8 @@ function getConverted(val, base) { return ret; } +// previous regex = /((-|\+)*[0-9]+)%/ +const getConvertedRegNum = /([-+]?[0-9.]+)%/; /** * Parse a transform atom value. @@ -75,7 +77,7 @@ function getConverted(val, base) { * which is called very frequently. */ function toParsedFloat(val) { - const m = val.match(/((-|\+)*[\d|.]+)(px|deg|rad)*/); + const m = val.match(toParsedFloatRegNum); let ret; if (m && m.length >= 1) { @@ -83,6 +85,8 @@ function toParsedFloat(val) { } return ret; } +// previous regex = /((-|\+)*[\d|.]+)(px|deg|rad)*/ +const toParsedFloatRegNum = /([-+]?[0-9.]+)(px|deg|rad)?/; function correctUnit(transform, width, height) { let m;