Original: A brief talk on JS regular literal and new RegExp execution efficiency
A few days ago, we talked about the problem of regular matching JS string: "JS regular learning small Remember the matching string" and "JS regular learning small memory matching string optimization."
It says that the optimization is the problem of improving the performance, but how much can be improved?
So I went to test, found that the TMD is almost negligible, I use 1 thousand strings for the match test, optimization is no difference.
This is not science, I have seen so many days, God this is playing with me.
Suddenly I thought of the compile method, and then went to test the next, the miracle appeared, sure enough to optimize a lot faster.
But why is that?
So I read the data and found RegExp Methods on the MDN.
Here says the compile method has been deprecated! This is not scientific ...
Found this article on StackOverflow Javascript:what's the point of Regexp.compile ()?
The main idea of the article is that the direct new REGEXP can be,compile almost no use.
So I changed the code one more time.
Sure enough, the direct new REGEXP effect and compile are the same.
But this is just the result of Nodejs, let's go and see what the results are in each browser.
<!DOCTYPE HTML><HTMLLang= "en"><Head><MetaCharSet= "UTF-8"><title>Test</title></Head><Body><Script>varstr1= '"'+Array ( -). Join ("x")+'123456\\\ ' 78\\\ '';varstr2= '"'+Array ( -). Join ("x")+'ooo\\\\oooo\\\nxxxx\\\ "xxxx"';//write here 100, small, prevent, such as half a day, FF that's half a dayConsole.log ("String 1:", str1); Console.log ("String 2:", str2);varREG1= /"(?:\ \.| [^"]) *"/;varREG2= /"(?: [^" \\]|\\[\d\d]) * "/;varreg11= NewRegExp ('"(?:\ \.| [^"]) *"');varReg22= NewRegExp ('"(?: [^" \\]|\\[\d\d]) * "');varN= 1e6; //1 million-time testTest ("REG1", REG1); Test ("REG2", REG2); Test ("reg11", reg11); Test ("Reg22", reg22);functionTest (name, re) {console.time (name); for (varI=0; I<N; I++{str1.match (re); Str2.match (re);} Console.timeend (name);}</Script></Body></HTML>
Chrome
Firefox
IE11
IE8 ( I called a plug-in simulation console to implement ie6-8 debug output )
Obviously, the first one is chrome (whichever is optimized by reg22), this is called to the teeth of the goods, is enough new force.
However, Firefox this goods, even IE8 than, is not too a little.
The optimized regular is faster than not optimized, that's for sure.
But the regular literal and the new RegExp ratio, that is not a grade.
Why is there such a big gap?
Actually, I don't know.
I've seen a lot of articles that say that the literal is more efficient than the new object form, but it doesn't seem to be the case here.
But can not directly deny this point of view, because I have been using the literal, concise and beautiful, with convenience is the kingly.
I think it is necessary to use new REGEXP when the volume of data is large or the number of repetitive operations is high.
Because you also see the performance gains so much.
Of course, the prerequisite is that your regular must be optimized, the situation is not optimized, two kinds of similar.
So optimizing your regular and then using new REGEXP can dramatically improve the performance of your program.
Ps:ie11 is a special case, the goods never disturbed the card.
Well, today's sharing is over, you are all ready, go to the regular various new up bar.