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

323
Visualizações
Which is more efficient code (parseInt several times or one time and put it in variable)?

I'd like to know if the first or the second way are more efficient and I am also asking about similar situations because they come up pretty often. It's all frontend code (React). I'd really like to know. Thanks!

(input) => {
  setState(parseInt(input, 10))
  updateAuction(parseInt(input, 10))
  ...

Or, saving the result of parseInt into a var and using the var

(input) => {
  const parsedInput = parseInt(input, 10)
  setState(parsedInput)
  updateAuction(parsedInput)
  ...

This and similar situations bother me every day, because I don't know the answer. I also have string comparisons quite often.

logoVariant === "small" && do something
logoVariant === "small" && do something else

or

const isLogoSmall = logoVariant === "small"
isLogoSmall && do something
isLogoSmall && do something else

Thanks!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It is always faster to cache the comparison result rather than doing a string comparison twice as the string comparison operation is much more complex than evaluating the cache result. This isn't as clear in languages with an advanced compiler like C++ where the compiler may be able to recognise the redundancy. The balance is making code less easy to read, and potentially increased memory usage if you are caching more complex results (although not in this case).

about 4 years ago · Santiago Trujillo 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