Answer the question
In order to leave comments, you need to log in
Caching a regular expression from a function?
Can v8, well, or other engines in the node optimize a function call with a regular expression? In general, which of the options is preferable.
one.
function isValidPropNumber(propNumber) {
return /^[1-9][0-9]{0,14}$/.test(propNumber);
};
const REGEX = /^[1-9][0-9]{0,14}$/;
heavyFunction() {
//... много кода
if (isValidPropNumber(something) || REGEX.test(something)) { };
//... еще много кода
}
Answer the question
In order to leave comments, you need to log in
The second one is preferable. I think that for v8 it is thousandths of a second to create this object again. And it will not be cached, because the object was created in the context of the function and after the function is executed, the reference to it will be lost, because the scope of the function is deleted after its execution.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question