Performance Of Regex Within JQuery Data Selector: Dependance On Certain String Length
Solution 1:
A characteristic of regexp matching is that they are greedy. If you try to match the expression a.*b
to the string abcd
, it will happen in these steps:
- the first "a" will match
- the .* will match the second char, then the third, till the end of the string
- reaching the end of the string, there is still a "b" to matched, the matching will fail
- the regexp processing starts to backtrack
- the last char will be "unmatched" and it will try to match "b" to "d". Fails again. More backtracking
- tries to match "b" to "c". Fail. Backtrack.
- match "b" to "b". Success. Matching ends.
Although you matched just a few chars, you iterated all the string. If you have more than one greedy operator, you can easily get an input string that will match with exponential complexity.
Understanding backtracking will prevent a lot of errors and performance problems. For example, 'a.*b' will match all the string 'abbbbbbb', instead of just the first 'ab'.
The easiest way to prevent these kind of errors in modern regexp engines, is to use the non-greedy version of the operators * and +. They are usually represented by the same operators followed by a question mark: *? and +?.
I confess that I really didn't stop to debug the complicate regexp that you posted, but I believe that the problem is before matching the '=' symbol. The greedy operator is in this subexpression:
(?:\\\.|[^.,])+\.?)+
I'd try to change it to a non-greedy version:
(?:\\\.|[^.,])+\.?)+?
but this is really just a wild guess. I'm using pattern recognition to solve the problem :-) It makes sense because it is backtracking for each character of the "value" till matching the operator. The name is matched linearly.
This regular expression is just too complex for my taste. I love regular expressions, but it looks like this one matches this famous quotation:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Post a Comment for "Performance Of Regex Within JQuery Data Selector: Dependance On Certain String Length"