javascriptregex

How often does JavaScript recompile regex literals in functions?


Given this function:

function doThing(values,things){
  var thatRegex = /^http:\/\//i; // is this created once or on every execution?
  if (values.match(thatRegex)) return values;
  return things;
}

How often does the JavaScript engine have to create the regex? Once per execution or once per page load/script parse?

To prevent needless answers or comments, I personally favor putting the regex outside the function, not inside. The question is about the behavior of the language, because I'm not sure where to look this up, or if this is an engine issue.


EDIT:

I was reminded I didn't mention that this was going to be used in a loop. My apologies:

var newList = [];
foreach(item1 in ListOfItems1){ 
  foreach(item2 in ListOfItems2){ 
    newList.push(doThing(item1, item2));
  }
}

So given that it's going to be used many times in a loop, it makes sense to define the regex outside the function, but so that's the idea.

also note the script is rather genericized for the purpose of examining only the behavior and cost of the regex creation


Solution

  • There are two "regular expression" type objects in javascript. Regular expression instances and the RegExp object.

    Also, there are two ways to create regular expression instances:

    1. using the /regex/ syntax and
    2. using new RegExp('regex');

    Each of these will create new regular expression instance each time.

    However there is only ONE global RegExp object.

    var input = 'abcdef';
    var r1 = /(abc)/;
    var r2 = /(def)/;
    r1.exec(input);
    alert(RegExp.$1); //outputs 'abc'
    r2.exec(input);
    alert(RegExp.$1); //outputs 'def'
    

    The actual pattern is compiled as the script is loaded when you use Syntax 1

    The pattern argument is compiled into an internal format before use. For Syntax 1, pattern is compiled as the script is loaded. For Syntax 2, pattern is compiled just before use, or when the compile method is called.

    But you still could get different regular expression instances each method call. Test in chrome vs firefox

    function testregex() {
        var localreg = /abc/;
        if (testregex.reg != null){
            alert(localreg === testregex.reg);
        };
        testregex.reg = localreg;
    }
    testregex();
    testregex();
    

    It's VERY little overhead, but if you wanted exactly one regex, its safest to only create one instance outside of your function