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

165
Visualizações
store easy retrievable pairs from both key and vaule in Javascript

I'm trying to find an efficient way to store pairs key:value pairs so that you can fast find the other value (while both are unique).

for example: lets say we wanna store pairs "unique_id: email"... and I want to be able to get the other value from either of the two values. What is the less resource consuming (both RAM and calc) way to do it?

Saving memory solution:

const data = {"1": "user1@email.com",
              "2": "user2@email.com",
              "3": "user3@email.com"}

var email1 = data['1']; //fast research
var user1 = data.find("user1@email.com") //slow research algorithm (find is some function that search through the item... probably in a more inefficient way than the above index-research.

efficient research solution?

const emails = {"1": "user1@email.com",
                "2": "user2@email.com",
                "3": "user3@email.com"}
                
const ids = { "user1@email.com": "1",
              "user2@email.com": "2",
              "user3@email.com": "3" }

var email1 = emails['1'];
var id1 = ids[ "user1@email.com"];

Now... in the second case we are consuming more memory, in the first we have a less efficient research (or is there some algorithm that is as fast as index-research?). For my application I actually have more than 2 unique values (like -and is just an example-id,gmail,microsoftid,passport_no). Of course with the second solution I'm using way more memory, and for my application memory usage is crucial.

So, what is the right way to do this? Is there some kind of js class that does this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use a vector of key-values, so it will be O(n), both for a key and a value.
But native object has O(1) and O(n), so it isn't a big deal. You could also make a ternary tree, when the left is both less than, the right is both greater than, middle is one is greater, the other is less (or 4ry tree, for better search), but dunno how to balance it well.

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