Skip to content

pMemoize

pMemoize(fn, «destructured»?): (...arguments_: any[]) => any

Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.

参数

NameTypeDescription
fnanyFunction to be memoized.
«destructured»Object-
› cacheundefined | Map<any, any>-
› cacheKeyundefined | (__namedParameters: [any]) => any-

返回值

fn

▸ (...arguments_): any

参数
NameType
...arguments_any[]
返回值

any

示例

import {setTimeout as delay} from 'node:timer/promises';
import pMemoize from 'p-memoize';
import got from 'got';

const memoizedGot = pMemoize(got);

await memoizedGot('https://sindresorhus.com');

// This call is cached
await memoizedGot('https://sindresorhus.com');

await delay(2000);

// This call is not cached as the cache has expired
await memoizedGot('https://sindresorhus.com');

源码

memoize.js