最近用到js作一些文本处理,免不了涉及正则表达式,因为文本的规模会达到GB级,速度和仍是很关键的。正则表达式
根据 jsperf 上的测试发现,若是须要用到正则去匹配的话,仍是预编译的表达式precompiled search
表现最好。这是一个比较容易也比较重要的优化项。express
看MDN发现有一个g flag
表明global match也就是尝试全部可能的匹配。MDN上的相关解释以下。jsp
Whether to test the regular expression against all possible matches in a string, or only against the first.性能
全部的又产生了一个疑问,若是我这须要判断是否存在一个表达式,不须要知道几个,也就是只用RegExp.test()
,需不须要g flag
,感受加上有可能会使速度变慢,可是不肯定,写了一个很简陋的性能测试。测试
var start = +new Date(), end, globalRegex = /someone/g, nonGlobalRegex = /someone/, testStr = 'This optimization makes the lexer more than twice as fast! Why does this make sense? First, if you think about it in the simplest way possible, the iteration over rules moved from Python code to C code (the implementation of the re module). Second, its even more than that. In the regex engine, | alternation doesnt simply mean iteration. When the regex is built, all the sub-regexes get combined into a single NFA - some states may be combined, etc. In short, the speedup is not surprising.someone'; for (var i = 100000; i >= 0; i--) { // with a g flag globalRegex.test(testStr); // without g flay // nonGlobalRegex.test(testStr); } end = +new Date(); console.log(end - start);
分别去掉注释分别运行发现带g flag
的须要25-30ms,而不带g flag
的却须要40+ms,和直觉相反。而后回想了一下g flag
的做用,接着看文档,发现一个叫作lastIndex
的属性:优化
The lastIndex is a read/write integer property of regular expressions that specifies the index at which to start the next match.ui
得知既然是尝试匹配全部可能,若是没有主动把lastIndex
清零,则会继续上一次的匹配知道结束。因此以上代码若是是带g flag
的状况上一次匹配完成,已经到了句末,加入此时console.log(globalRegex.lastIndex)
会获得testStr.length
,并且下一次会继续尝试向后匹配,并另计返回false。因此能够理解上述的时间差。this
假如把for循环中带g flag
的状况加一句:code
for (var i = 100000; i >= 0; i--) { // with a g flag globalRegex.test(testStr); globalRegex.lastIndex = 0; // without g flay // nonGlobalRegex.test(testStr); }
两种状况的运行结果都在40+ms。regexp
结论:即便加上g flag
理论上也不影响速度,只须要将lastIndex
清零,不过清零仍是须要消耗的,因此若是只须要匹配判断,能够不用g flag