Need help with p-memoize?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

sindresorhus
159 Stars 17 Forks MIT License 21 Commits 2 Opened issues

Description

Memoize promise-returning & async functions

Services available

!
?

Need anything else?

Contributors list

# 80
Shell
exec
test-fr...
nextjs
14 commits
# 7,406
TypeScr...
iterm2
Node.js
exec
2 commits
# 191,779
Swift
coreloc...
Shell
Sass
1 commit
# 646,650
TypeScr...
JavaScr...
1 commit
# 88,875
C#
Shell
Node.js
interva...
1 commit
# 7,332
TypeScr...
Visual ...
vscode
stringi...
1 commit
# 6,862
chalk
iterm2
exec
Electro...
1 commit

p-memoize

Memoize promise-returning & async functions

Useful for speeding up consecutive function calls by caching the result of calls with identical input.

By default, only the memoized function's first argument is considered and it only works with primitives. If you need to cache multiple arguments or cache

object
s by value, have a look at options below.

Install

$ npm install p-memoize

Usage

const pMemoize = require('p-memoize');
const got = require('got');

const memGot = pMemoize(got, {maxAge: 1000});

(async () => { memGot('https://sindresorhus.com');

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

setTimeout(() => {
    // This call is not cached as the cache has expired
    memGot('https://sindresorhus.com');
}, 2000);

})();

API

pMemoize(fn, options?)

Returns a memoized version of the

fn
function.

fn

Type:

Function

Promise-returning or async function to be memoized.

options

Type:

object

See the

mem
options in addition to the below option.

cachePromiseRejection

Type:

boolean
\ Default:
false

Cache rejected promises.

pMemoize.clear(memoized)

Clear all cached data of a memoized function.

Will throw if passed a non-memoized function.

Related

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.