Thursday, November 13, 2025
HomeLanguagesJavascriptLodash _.memoize() Method

Lodash _.memoize() Method

Lodash _.memoize() method is used to memorize a given function by caching the result computed by the function. If the resolver is issued, the cache key for storing the result is determined based on the arguments given to the memoized method. By default, the first argument provided to the memoized function is used as the map cache key.

Syntax:

_.memoize(func, [resolver]);

Parameters:

  • func: This parameter holds the function to have its output memoized.
  • resolver: It is the function to resolve the cache key.

Return Value:

This method returns the new memoized function. 

Example 1: In this example, we are printing the sum of the first 6 natural numbers by the use of the _.memoize() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Use of _.memoize() method 
let sum = _.memoize(function (n) {
    return n < 1 ? n : n + sum(n - 1);
});
 
// Sum of first 6 natural number 
console.log(sum(6));


Output:

21

Example 2: In this example, we are printing the values of the object by the use of the _.memoize() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
let object = { 'cpp': 5, 'java': 8 };
 
// Use of _.memoize() method 
let values = _.memoize(_.values);
 
// value of object
console.log(values(object));
 
// Modify the result cache.
values.cache.set(object, ['html', 'css']);
console.log(values(object));


Output:

[5, 8]
['html', 'css']
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11983 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6834 POSTS0 COMMENTS
Umr Jansen
6838 POSTS0 COMMENTS