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

201
Visualizações
JS data structure for O(1) caching when checking cache keys by value

I want to create a cache where I have O(1) lookups to its contents, and I lookup keys by value, not by reference. What data structure in JS, if any, would let me accomplish this?

Requirements:

  1. The keys represent an array of valid JS variables (primitives or objects).
  2. Comparison is handled by value, not by reference. (If it was reference, we could just use a single Map)

What I've tried:

I was thinking of nested Maps following this structure:

const resultKey = new Symbol('result'); // Create a unique result key, so we don't accidentally return if a key happens to be called 'result'. 

// Cache is nested maps, not objects.
const cache = {
  [key1]: {
    [key2]: {
      [key3]: {
        [resultKey]: 1234
      }
    }
  }
}

const foo = function cachedFunc(key1, key2, key3); // If these keys match values in the cache, just return the cache value. 

And this would work fine for O(1) lookups by reference, but for value I would still need to iterate the keys at each level, and do a deep equality check.

Any ideas how I can get an O(1) lookup by value?

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

0

seems the most suitable data structures for your task are HashMap and Set (based on HashMap). The average time for insert and getting is O(1) https://adrianmejia.com/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial

about 4 years ago · Juan Pablo Isaza Relatório

0

Would you like to try serialization with hash alongside with your value? I mean, something like:

const cache = {
  [key1]: {
    [key2]: {
      [key3]: {
        [resultValue]: { a: 5, b: 6 },
        [resultHash]: md5(JSON.stringify(val))
      }
    }
  }
}

Browsers doesn't have any built-in hash functions API. So, get it from npmjs.org


UPDATE:

I may have misunderstood your question. What about this implementation?

const cache = new Map()
const hash = data => btoa(JSON.stringify(data))
const hashKeys = (...keys) => keys.map(key => hash(key)).join('-')
const store = (data, ...keys) => cache[hashKeys(...keys)] = data
const load = (...keys) => cache[hashKeys(...keys)]
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