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.
Thank you very much for your PR!
As you mentioned above, there was regex insufficiency. And your suggestion looks better than previous. 👍 But as count of parentheses is different from previous one, match result of regex also changed. So functions depend on that regex should be changed also. If not changed, it will not work.
In following code m[3] is no more valid, it should be m[2].
function toParsedFloat(val) {
const m = val.match(toParsedFloatRegNum);
let ret;
if (m && m.length >= 1) {
ret = {"num": parseFloat(m[1]), "unit": m[3]}; // m[3] is no more valid, it should be m[2]
}
return ret;
}
And related with the performance, getParsedFloat and getConverted are heavy function as you pointed. But it is not called in loop.
BecauserateFn does not work in loop. rateFn returns function which is used in loop.
fx.rateFn = fx.rateFn || rateFn(fx.elem, fx.start, fx.end); // fx.rateFn is not rateFn
Same name may be confusing your code reading. Sorry for confusing name. TT
And as I know inline regex is not generating reg expression each time. (although RegEx is generating each time.) But Caching inline regex (your suggestion) is more better (https://jsperf.com/regexp-indexof-perf).
Finally It would be thankful if you check eslint and test as following. :)
npm run lint // for eslint
npm run test // for unit test
After review applied, I'll merge your precious PR! Thank you again.
I apologize for requests without a lint & test. Next time, prepare more for the guide ^^;
Please close it.