jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
20 stars 3 forks source link

55 - memo - javascript #57

Open jsartisan opened 2 months ago

jsartisan commented 2 months ago

index.js

export function memo(func) {
  const cache = {};

  return function(...args) {
    const key = JSON.stringify(args);

    if (!(key in cache)) {
      cache[key] = func.apply(this, args);
    }

    return cache[key];
  }
}