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

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