egjs-jquery-transform icon indicating copy to clipboard operation
egjs-jquery-transform copied to clipboard

refector(rateFn): RegularExpression optimize

Open hikaMaeng opened this issue 8 years ago • 3 comments

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.

hikaMaeng avatar Aug 18 '17 05:08 hikaMaeng

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.

jongmoon avatar Aug 18 '17 07:08 jongmoon

I apologize for requests without a lint & test. Next time, prepare more for the guide ^^;

hikaMaeng avatar Aug 19 '17 18:08 hikaMaeng

Please close it.

hikaMaeng avatar Aug 19 '17 18:08 hikaMaeng