Gathering detailed insights and metrics for lazy-memoize-one
Gathering detailed insights and metrics for lazy-memoize-one
Gathering detailed insights and metrics for lazy-memoize-one
Gathering detailed insights and metrics for lazy-memoize-one
npm install lazy-memoize-one
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7 Stars
23 Commits
22 Watching
3 Branches
8 Contributors
Updated on 09 Feb 2022
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-57.1%
3
Compared to previous week
Last month
480%
58
Compared to previous month
Last year
31.2%
122
Compared to previous year
1
4
A wrapper around memoize-one
for use with asynchronous functions, that resolves their values
asynchronously while returning synchronously.
memoize-one
is a great library until you consider how to use it with asynchronous results:
1import memoizeOne from 'memoize-one'; 2 3const add = async (a, b) => a + b; 4const memoizedAdd = memoizeOne(add); 5 6memoizedAdd(1, 2); // Promise
The memoization still worksāif you call memoizedAdd(1, 2)
again it will return the same
Promise
, without executing add
another timeābut it's not very usable anymore: every
call to memoizedAdd
will have to await
the result of the promise.
In some cases, it's easier to deal with the result synchronously, if and when it's available; and
if it's not, retry later. For example, memoize-one
is often used in React components, where
as of React 16.4 render
executes synchronously, but components can rerun render
at any time
by calling setState
.
In these cases, you have lazy-memoize-one
:
1import memoizeOne from 'lazy-memoize-one'; 2 3const add = async (a, b) => a + b; 4const memoizedAdd = memoizeOne(add); 5 6memoizedAdd(1, 2); // `undefined` 7 8// Wait a turn of the event loop, for `add`'s return value to resolveā¦ 9await Promise.resolve(); 10 11memoizedAdd(1, 2); // 3
You can even pass a function as the last argument to the memoized function, to let you know when the result is ready:
1memoizedAdd(2, 3, (err) => { 2 if (err) { 3 console.error('Failed to add 2 + 3 for some reason:', err); 4 } else { 5 console.log(`2 + 3 is ${memoizedAdd(2, 3)}`); 6 } 7});
1npm install lazy-memoize-one
First, create a memoized function just like memoize-one
, by passing a function and (optionally)
a custom equality function:
1import memoizeOne from 'lazy-memoize-one'; // or `const memoizeOne = require('lazy-memoize-one');` 2 3const add = async (a, b) => a + b; 4const memoizedAdd = memoizeOne(add);
Note that, in contrast to memoize-one
, the original function being memoized must be an async
function. (If it's not, why are you using this library? You'll get synchronous results by using
regular memoize-one
.)
Now call the memoized function with the same arguments as you would call the original function.
You won't get a Promise
back, though. Instead, the memoized function will resolve the promise
behind the scenes while returning undefined
, then return the value with which the promise
resolves:
1memoizedAdd(1, 2); // `undefined` 2 3// Wait a turn of the event loop, for `add`'s return value to resolveā¦ 4await Promise.resolve(); 5 6memoizedAdd(1, 2); // 3
If the promise is rejected, memoizedAdd
will continue to return undefined
.
If you want to know when the promise settles, you can pass a function as the last argument to the memoized function. This function will be called when the promise is resolved or rejected:
1memoizedAdd(2, 3, (err) => { 2 if (err) { 3 console.error('The promise was rejected with error:', err); 4 } else { 5 console.log(`2 + 3 is ${memoizedAdd(2, 3)}`); 6 } 7});
This function will not be called if the memoized function was called with different arguments before the promise was settled, however, primarily because you wouldn't be able to retrieve the promise's result at that point anyway (it will no longer be in the cache).
memoize-one
Read the rationale for the biggest difference from memoize-one
: the ability to memoize
asynchronous functions but work with their results synchronously.
Other differences:
the original function being memoized must be an async
function. (If it's not, why are you
using this library? You'll get synchronous results by using regular memoize-one
.)
there is caching when your result function throws. Specifically, the previously-cached value
(if any) will be discarded, and the memoized function will return undefined
. Furthermore, the
memoized function will not execute the original function a second time if the memoized function
is next called with the same arguments.
This is primarily a consequence of using memoize-one
(which this library does, internally) to
cache promises, since this means updating the cache before it's known whether doing so will fail.
For what it's worth, you would have the same difficulty if you were to use memoize-one
directly
with async functions.
lazy-memoize
lazy-memoize
differs from this library in two big ways. The first is that it caches all the
calls you make of it, not just the latest one.
The second is that it forces you to await
the cache being initialized. The "lazy" part of this
library is that it returns undefined
while it's loading, synchronously; then returns the value,
also synchronously. The "lazy" part of lazy-memoize
is that it expires the initial values
eventually and then fetches updated values asynchronously, while returning the previous value
more quickly.
This is unacceptable for this library's use case, where access always has to be synchronous, and it
is ok for the cache to temporarily return undefined
.
We welcome bug reports and feature suggestions. PRs are even better!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/12 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
60 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More