I have a email validation fuction where i need to trim string value twice.
(val) => (val.trim() && emailReg.test(val.trim()))
Or
(val) => {
const value = val.trim();
return value &&emailReg.test(value)
}
which is more faster and meet coding standards.
It really depends on personal preference, so either one is fine (as long as they both work).
However, the most performant option is the second option.
This is because you are only trimming the string once, which will save a few milliseconds when returning the value.
const emailReg = undefined; // Email RegEx
const emailTest = (val) => {
const value = val.trim(); // Only trimming once
return value && emailReg.test(value) // Passing both of the variables
}
Edit: However, it may take a few milliseconds when making a new memory reference for the variable.
Again, choose whichever you like (mainly depends on personal preference).