1

I take care to declare a RegEx pattern once and reuse if possible, for performance reasons. I'm not entirely certain why - something I probably read once many years ago and has been filed away in the ol' skull sponge.

I find myself in a regex-heavy situation, and a thought occurred... does declaring a RegEx pattern "instantiate" or "initialize" that pattern, or does it just store the pattern until it's needed?

var NonNumbers = /[^0-9]/g; //"initialized" here?
"h5u4i15h1iu".replace(NonNumbers, "*"); //or "initialized" here?

Maybe RegExp() actually creates one and the literal waits until it's used, even though both patterns return the same results?

var NonNumbers = /[^0-9]/g; //just stores the pattern
var NonNumbers = RegExp(/[^0-9]/, 'g'); //actually creates the RegExp

Just an itch I'm hoping someone who understands the inner workings can scratch.

10
  • 2
    on declaration, so re-using named vars instead of hard-coding literals will improve perf. Commented Jun 27, 2017 at 18:24
  • 3
    In case if you are using .exec there will be a difference, because when using .exec the regex stores an index from which it will start at next call. Commented Jun 27, 2017 at 18:24
  • 2
    Literal is just syntactic sugar, right? They're both RegExp objects. Commented Jun 27, 2017 at 18:26
  • 2
    Side note: /[^0-9]/ is equivalent to /\D/ Commented Jun 27, 2017 at 18:28
  • 2
    When it encounters the statement var Rx = /[^0-9]/; it creates a regex instance. I think, but not sure, something like string.replace(/[^0-9]/, ""); creates a new object on the stack each time. Commented Jun 27, 2017 at 18:29

1 Answer 1

3

From the Mozilla spec:

You construct a regular expression in one of two ways:

Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:

var re = /ab+c/;

Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.

Or calling the constructor function of the RegExp object, as follows:

var re = new RegExp('ab+c');

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

Since the spec indicates that the regular expression is being compiled when using the literal syntax, it is also safe to assume that it is being initialized as a full, bona-fide regular expression object at that point.

Another advantage of using literals is that regular expressions can be interned, meaning that if the same regular expression literal is found in multiple places, both literals can refer to the same object, saving both memory and initialization costs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.