DEV Community

Discussion on: Memoization in JavaScript

Collapse
 
shadowtime2000 profile image
shadowtime2000

You could also use a Map for that.

const memoize = (callback) => {
    const cache = new Map();

    return (...parameters) => {
        if (typeof cache.get(parameters) !== "undefined") {
            return cache.get(parameters);
        }
        cache.set(parameters, callback(...parameters));
        return cache.get(parameters);
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
radulle profile image
Nikola Radulaški

Why not just cache.get(parameters) !== undefined?

Collapse
 
bhagatparwinder profile image
Parwinder 👨🏻‍💻

👏🏼 I like this