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.