22

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?

2
  • You can simply try it out: jsfiddle.net/HBNXF/1
    – Passerby
    Commented Jan 16, 2013 at 7:17
  • You can't easily try that out -- the results are the same with or without caching. To try this, you'd need to construct some regexp that takes a long time to compile and time somehow whether the second call is shorter or not.
    – chx
    Commented Sep 19, 2013 at 9:31

2 Answers 2

19

Well, let's find out.

// http://stackoverflow.com/questions/3466850/complex-password-regular-expression
function isFooBar(str) {
  return str.match(/^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}(?:(?=.*[a-z])(?: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*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$)?(^(?=.*\d)(?=.*[a-z])(?=.*[@#$%^&+=]).*$)?(^(?=.*\d)(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?(^(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$)?$/);
}
console.time('1');isFooBar("Foo");console.timeEnd('1');
console.time('2');isFooBar("Bar");console.timeEnd('2');

(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):

1: 65ms
2: 0ms

On io.js 1.6.3 (V8 4.1.0.27):

1: 32ms
2: 0ms

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 or 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.

3
  • I just asked the same question here, marked as duplicated of this one. I'm also curious to know why many of the greatest (and fastest) Node.js libs move regexp outside of the function using it. Old habit?
    – cronvel
    Commented Sep 17, 2015 at 12:26
  • @cronvel I just added some more information on that to the answer. In case it is not clear what that means: probably partially old habit, partially not wanting to rely on V8's optimization heuristics that may change any time.
    – Timothy Gu
    Commented Feb 25, 2017 at 4:45
  • 1
    @TimothyGu I tested with using new RegExp() and the resulting runtimes are similar to using regex literal, so it contradicts the above claim
    – Amy Guo
    Commented Mar 12, 2021 at 20:12
0

http://code.google.com/p/v8/issues/detail?id=933#c3

In Chrome 9 we have removed a primitive caching of RegExp results. That means that we no longer take unrealistically low time for repeating the same operation on the same input over and over again. The caching was originally added to make a point about benchmarks that were too simplistic (our own, at that time, included) being too easy to "game" by adding such caching (which makes no difference in real-world scenarios). Most benchmarks have since improved so much that we decided to remove the caching (and its unavoidable overhead). The cache was added in v8 revision 4083 (released in Chrome 5) and removed in revision 5755 (will be in Chrome 9)

1
  • 13
    Reading further down in the bug, this doesn't answer the question. The cache they're talking about caches the results of regexs, not the compiled regexes themselves. Hence, it only speeds up repeated searching of the exact same string by the same regex, as a poke at benchmark authors. It's not stated in this bug whether or not compiled regexes are cached.
    – David M.
    Commented Sep 26, 2013 at 18:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.