Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda