Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda