So I know that in Javascript instantiating a regex causes it to be compiled automatically and if that regex is stored in a variable it will be reused without recompiling. Example:
var re = /[Ff]oo|[Bb]ar/;
"Foo".match(re); // ["Foo"]
"Baz".match(re); // null
However, are duplicated regular expressions recompiled or does V8 (Chrome/Node.js) intelligently re-use them (like PHP does with preg_* functions)?
function isFooBar(str) {
return str.match(/[Ff]oo|[Bb]ar/);
}
isFooBar("Foo"); // ["Foo"]
isFooBar("Baz"); // null
So to clarify, I know the first example results in one compilation of the regex. But what about the second example?
Well, let's find out.
(Open your console and run the code snippet above to see if the results for your browser.)
On Node.js 0.10.36 (V8 3.14.5.9):
On io.js 1.6.3 (V8 4.1.0.27):
So, I guess the answer to your question is YES, V8 is capable of automatically caching the regex.
EDIT:
Even though V8 is capable of such optimization, it might not in fact choose to always use this optimization. V8 has many complex heuristics for different cases, like when to inline a function, when to optimize a function, when to de-optimize a function, etc. Unless you are a V8 developer, many of these heuristics would appear to be bizarre or unexpected or both.
Not being a V8 developer myself, I do not know the answer to the specific question of if regexes are always automatically cached. However, even if I do know the answer, it may change in the future. So when you are writing code, I would still recommend storing the regex in such a way that it is clear what the intention for this variable is. And for the example above, this means lifting the regex variable out of the function
isFooBar()
.Another issue is how this regex is created. The example above and in OP's question are declared with a regular expression literal. However, if you are using the
new RegExp()
constructor, V8 would not be allowed to cache the regex since it might change during runtime.http://code.google.com/p/v8/issues/detail?id=933#c3