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

143
Visualizações
Memoize function with all types of arguments support

Is there any way we can create a very generic function that supports all arguments?

function memoize(expensivefn) {
  const map = new Map();

  // args can have array, object, functions and any other data types
  return function(...args) {
    if (map.has(args)) return map.get(args);

    const result = expensivefn(...args);

    map.set(args, result);
    return result;
  };
}

const expensiveFunction = args => {
  console.log("A very expensive function, can block the tread!");
};

const memfun = memoize(expensiveFunction);

// calling function with named function
function add(a, b) {
  return a + b;
}
memfun(add); // invoke the expensive function

memfun(add); // return from cache


// calling function with anonymous function
memfun(() => {
  console.log("print someting!");
}); // invoke the expensive function

memfun(() => {
  console.log("print someting!");
}); // return from cache


// Calling function with array and object

memfun([1, 2, 4, 4], { name: "abc" }); // invoke the expensive function

memfun([1, 2, 4, 4], { name: "abc" }); // return from cache
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