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

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 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