2

Recently I have been so performance paranoid (I read those scary programming articles), that I started feeling that there is a bit performance difference between storing and accessing variable with static value and using that value on the go where needed;

for e.g;

function check(name) {
    var match = name.match(/^Donald Middlename Trump$/i);
    if(!match) match = name.match(/^Hillary Mrs Clinton$/i);
    if(!match) match = name.match(/^Obama$/i);
    // e.t.c

    return match;
}

My paranoia in the above function is that (correct me if am wrong, because /^$/ == /^$/ // false) I think that an instance of the RegExp object is created for the three regex, every time the check function is fired, ergo Javascript taking some time to create it every time. Though there is just one place each regex is used, but I feel the code will perform better by creating the regex once then referencing it from there on.

for e.g.

var trump = /^Donald Middlename Trump$/i,
    hillary = /^Hillary Mrs Clinton$/i,
    obama = /^Obama$/i;

function check(name) {
   var match = name.match(trump),
   if(!match) match = name.match(hillary);
   if(!match( match = name.match(obama);

   return match;
}

Long question short, is there a performance difference or benefit between accessing a variable and recreating the object.

Eddie Dane
  • 1,321
  • 3
  • 13
  • 22
  • @ibrahimmahrir That's incorrect. –  Apr 29 '17 at 12:16
  • @Hassan That's incorrect. –  Apr 29 '17 at 12:16
  • @ibrahimmahrir You're confusing **parsing** with **execution**. The code is parsed once. The regexp is converted into an internal form exactly once, when the code is parsed. –  Apr 29 '17 at 12:20
  • @torazaburo When I said **parsing** I meant parsing the regexp not the whole code. But I think you're right. – ibrahim mahrir Apr 29 '17 at 12:21

2 Answers2

1

Yes, there is a performance difference, and the codes will work differently (e.g. when it uses the lastIndex property). However, don't become paranoid about it - premature optimisation is the root of all evil. You should focus on correct and readable code.

If there is any relevant performance gain by this, leave it to the compiler. Hoisting and constant propagation are two relatively simple optimisations that will be done automatically.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @torazaburo I meant that it does different things, so it will have a different performance - no more and no less. I guess I made it easy to misunderstand, I did not want to indicate a performance *benefit*. I have no idea which code will be faster, or whether the difference is significant at all (though I could make an educated guess), as usual the only way to know is to benchmark. – Bergi Apr 29 '17 at 13:01
0

My paranoia in the above function is that (correct me if am wrong, because /^$/ == /^$/ // false) I think that an instance of the RegExp object is created for the three regex, every time the check function is fired, ergo JavaScript taking some time to create it every time.

The engine no more creates a new instance of the RegExp object each time the function is invoked, than it would create a new instance of the number 5 if that were present literally in the function.

Do not confuse parsing with execution. The code is parsed exactly once. The regexp is analyzed and stored into an internal form (compiled, if you will) at parsing time.

The only reason to choose one approach over the other is readability.

  • @ibrahimmahrir Technically speaking, "caching" is not exactly the right term, but conceptually yes, it is pre-storing compiled versions of the regexp literal at parse time. –  Apr 29 '17 at 12:22