admin管理员组

文章数量:1287216

In this post it's said that prepiling your regular expressions will improve script performance. The author proves it by performance test. However, as far as I understand, the post is talking about the cases when you use your regular expressions repeatedly. What if there are lots of regular expressions in the script, but each is used only once? Will there be a performance benefit in prepiling regex which is used only once throughout the script?

In this post it's said that prepiling your regular expressions will improve script performance. The author proves it by performance test. However, as far as I understand, the post is talking about the cases when you use your regular expressions repeatedly. What if there are lots of regular expressions in the script, but each is used only once? Will there be a performance benefit in prepiling regex which is used only once throughout the script?

Share Improve this question asked Feb 18, 2013 at 23:38 tch3rtch3r 631 silver badge4 bronze badges 1
  • You may get some perceived benefit by piling up front while the page is loading and users expect a bit of slowness. But that's just shifting the work to some other time, it doesn't save anything and the difference will likely be imperceptible. – RobG Commented Feb 18, 2013 at 23:50
Add a ment  | 

3 Answers 3

Reset to default 4

I don't believe the performance test that you linked is conclusive. If you look at the results, the difference is negligible because the regex isn't plex enough. Take a look at this test for a little better answer.

Either way, storing a regex value will only provide a performance increase if the regex is used more than once. This performance increase is solely due to the initial pilation overhead for the regex itself. If you are storing the regex in a variable then it will still be piled the first time just as a literal will be piled the first time. The difference happens when the stored regex is used a second time and it is already piled whereas the literal regex would have to be piled again.

I think this depends on browser implementation and we cannot conclusively say one approach is better.

See the different results in firefox and chrome.

I am confused why chrome gives faster results for non-repiled regex though.

If it's only used once - then just use regexp literal.

Your point is valid - it only makes sense when you use the same regular expression a lot.

本文标签: JavascriptPrecompiled regex performanceStack Overflow