Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!