Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Trim twice or trim and assign to a variable which will be more faster in perfomance, javascript

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!