Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of js regular literals / / and the execution efficiency of new RegExp

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Analysis of js regular literals / / and new RegExp implementation efficiency", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "analyze js regular literals / / and new RegExp implementation efficiency" bar!

With regard to the question that optimization regularization can improve performance, how much can it improve?

So I went to test and found that TMD was almost minimal. I did 1 million matching tests with a thousand strings, and it made no difference whether you were optimized or not.

It's not scientific. I've been reading the rules for so many days. God is playing with me.

All of a sudden, I thought of the compile method, and then I went to test it. A miracle happened, and sure enough, it was optimized a lot faster.

But why?

So I looked through the materials and found RegExp Methods on MDN.

It says here that the compile method has been deprecated! It's not scientific.

Find this article on stackoverflow _ Javascript: what's the point of RegExp.compile ()?

The main idea of the article is that new RegExp can be done directly, and compile can hardly be used.

So I changed the code and do it again.

Sure enough, the direct new RegExp effect is the same as compile.

However, this is only the result of nodejs, let's take a look at the results of each browser.

Test var str1 ='"+ Array (100). Join (" x ") + '123456\\' 78\\" 90 "; var str2 ='"+ Array (100,100). Join (" x ") + 'ooo\\ oooo\\ nxxxx\\" xxxx "; / / write 100,100 here, smaller to prevent waiting for half a day, FF will console.log for half a day (" string 1: ", str1) Console.log ("string 2:", str2); var reg1 = / "(?:\\. | [^"]) * "/; var reg2 = /" (?: [^ "\\] |\\ [\ d\ D]) *" /; var reg11 = new RegExp ('"(?:\\. | [^"]) * "); var reg22 = new RegExp ('" (?: [^ "\\] |\\ [\ d\ D]) *"); var n = 1e6 / 1 million tests for test ("reg1", reg1); test ("reg2", reg2); test ("reg11", reg11); test ("reg22", reg22); function test (name, re) {console.time (name); for (var I = 0; I)

< n; i++) { str1.match(re); str2.match(re); } console.timeEnd(name); }

Chrome

Firefox

IE11

IE8 (I called a plug-in emulation console to implement IE6-8 debug output)

Obviously, the first place is chrome (whichever is optimized by reg22), this so-called armed to the teeth of the goods, really enough new.

However, the firefox can not even compete with the IE8. Isn't that a bit of a pussy?

Optimized regularities are faster than unoptimized ones, that's for sure.

But regular literal and new RegExp ratio, that is not the same grade.

Why is there such a big gap?

Actually, I don't know.

I've seen a lot of articles saying that literals are more efficient than new object forms, but in the regular case, that doesn't seem to be the case.

However, I can not directly deny this view, because I have always used literal, concise and beautiful, convenient is the king.

I think it is necessary to use new RegExp when there is a large amount of data or a large number of repeated operations.

Because you've seen so much performance improvement.

Of course, the prerequisite is that your regularities must be optimized, and if the regularities are not optimized, the two are similar.

So optimizing your rules and then using new RegExp can greatly improve the performance of the program.

PS: IE11 is a special case, this product is never easy to play tricks.

Thank you for your reading, the above is the content of "analyzing the implementation efficiency of js regular literals / / and new RegExp". After the study of this article, I believe you have a deeper understanding of the analysis of js regular literals / / and the implementation efficiency of new RegExp, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report