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

156
Visualizações
Is it more efficient to convert to string than to iterate an object to find something?

I have a dilemma of which case is more efficient.
I have a javascript array with this structure

elements = 
  [ { id: 'uuid',         children: [] } 
  , { id: 'uuid',         children: [] } 
  , { id: 'uuid',         children: 
      [ { id: 'uuid',     children: [] } 
      , { id: 'uuid',     children: 
          [ { id: 'uuid', children: [] } 
          , { id: 'uuid', children: [] } 
          ] 
        } 
      , { id: 'uuid',     children: [] } 
      ] 
    } 
  ] 

To find a specific object, I have two ways of working, one is to iterate each element until I find it and the other is to convert it to string and if the id exists in the children, I enter the node

function findElementById(id_, elements){
for (el of elements){
    if(el.id == id_) return el
    if(JSON.stringify(el.children).includes(id_)) return findElementById(id, el.children)
}
}

function findElementById(id_, elements){
for (el of elements){
    if(el.id == id_) return el
    if(el.children.length > 0) result = findElementById(id, el.children)
    if (result) return result
}
}

It's efficient to convert to string to avoid entering nodes that will not return anything or in cases where the object is very large, converting to string uses a lot of resources.

about 4 years ago · Juan Pablo Isaza
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