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
3 Answers
Reset to default 4I 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
版权声明:本文标题:Javascript - Precompiled regex performance - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741307046a2371431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论