Home

function: memoize


memoize(func: function, resolver: function): function

Creates a function that memoizes the result of `func`. If `resolver` is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key. The `func` is invoked with the `this` binding of the memoized function. **Note:** The cache is exposed as the `cache` property on the memoized function. Its creation may be customized by replacing the `memoize.Cache` constructor with one whose instances implement the [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object) method interface of `clear`, `delete`, `get`, `has`, and `set`.

Example(s):

const object = { 'a': 1, 'b': 2 }
const other = { 'c': 3, 'd': 4 }

const values = memoize(values)
values(object)
// => [1, 2]

values(other)
// => [3, 4]

object.a = 2
values(object)
// => [1, 2]

// Modify the result cache.
values.cache.set(object, ['a', 'b'])
values(object)
// => ['a', 'b']

// Replace `memoize.Cache`.
memoize.Cache = WeakMap

Params:

Name Type Attribute Description
func The function to have its output memoized.
resolver The function to resolve the cache key.

Returns:

function

  {
    "comment": "/**\r\n * Creates a function that memoizes the result of `func`. If `resolver` is\r\n * provided, it determines the cache key for storing the result based on the\r\n * arguments provided to the memoized function. By default, the first argument\r\n * provided to the memoized function is used as the map cache key. The `func`\r\n * is invoked with the `this` binding of the memoized function.\r\n *\r\n * **Note:** The cache is exposed as the `cache` property on the memoized\r\n * function. Its creation may be customized by replacing the `memoize.Cache`\r\n * constructor with one whose instances implement the\r\n * [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)\r\n * method interface of `clear`, `delete`, `get`, `has`, and `set`.\r\n *\r\n * @since 0.1.0\r\n * @category Function\r\n * @param {Function} func The function to have its output memoized.\r\n * @param {Function} [resolver] The function to resolve the cache key.\r\n * @returns {Function} Returns the new memoized function.\r\n * @example\r\n *\r\n * const object = { 'a': 1, 'b': 2 }\r\n * const other = { 'c': 3, 'd': 4 }\r\n *\r\n * const values = memoize(values)\r\n * values(object)\r\n * // => [1, 2]\r\n *\r\n * values(other)\r\n * // => [3, 4]\r\n *\r\n * object.a = 2\r\n * values(object)\r\n * // => [1, 2]\r\n *\r\n * // Modify the result cache.\r\n * values.cache.set(object, ['a', 'b'])\r\n * values(object)\r\n * // => ['a', 'b']\r\n *\r\n * // Replace `memoize.Cache`.\r\n * memoize.Cache = WeakMap\r\n */",
    "meta": {
        "range": [
            1506,
            2083
        ],
        "filename": "memoize.js",
        "lineno": 45,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100006103",
            "name": "memoize",
            "type": "FunctionDeclaration",
            "paramnames": [
                "func",
                "resolver"
            ]
        },
        "vars": {
            "memoized": "memoize~memoized",
            "": null,
            "memoized.cache": "memoize~memoized.cache"
        }
    },
    "description": "Creates a function that memoizes the result of `func`. If `resolver` is\rprovided, it determines the cache key for storing the result based on the\rarguments provided to the memoized function. By default, the first argument\rprovided to the memoized function is used as the map cache key. The `func`\ris invoked with the `this` binding of the memoized function.\r\r**Note:** The cache is exposed as the `cache` property on the memoized\rfunction. Its creation may be customized by replacing the `memoize.Cache`\rconstructor with one whose instances implement the\r[`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)\rmethod interface of `clear`, `delete`, `get`, `has`, and `set`.",
    "since": "0.1.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Function",
            "value": "Function"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "The function to have its output memoized.",
            "name": "func"
        },
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "optional": true,
            "description": "The function to resolve the cache key.",
            "name": "resolver"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "Returns the new memoized function."
        }
    ],
    "examples": [
        "const object = { 'a': 1, 'b': 2 }\rconst other = { 'c': 3, 'd': 4 }\r\rconst values = memoize(values)\rvalues(object)\r// => [1, 2]\r\rvalues(other)\r// => [3, 4]\r\robject.a = 2\rvalues(object)\r// => [1, 2]\r\r// Modify the result cache.\rvalues.cache.set(object, ['a', 'b'])\rvalues(object)\r// => ['a', 'b']\r\r// Replace `memoize.Cache`.\rmemoize.Cache = WeakMap"
    ],
    "name": "memoize",
    "longname": "memoize",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000571",
    "___s": true,
    "filepath": "memoize.js"
}