
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
promise-mem
Advanced tools
Memoizes a promise-returning function.
After using Sindre Sorhus' p-memoize
for a long time, it was time to build an API that was going to handle a ton of load and had to perform many asynchronous calls to fulfill every request.
And p-memoize
was the choice #1 to manage the load and improve the performance.
But soon after a problem was observed: the initial set of calls that hit the (cold) cache would trigger multiple parallel asynchronous calls until the cache was loaded with data.
From that point forward, it handled the calls as expected.
As this was not acceptable and debugging p-throttle
did not result in any finding, a custom implementation that properly managed that scenario was the next choice: promise-mem
.
npm install promise-mem
const pMemoize = require('promise-mem')
const memoized = pMemoize(asyncCall)
memoized()
.then(function (value) {
// `asyncCall` was called and it returned `value`.
})
.then(() => memoized())
.then(function (value) {
// `asyncCall` was NOT called and `value` was read from the cache.
})
Returns a memoized function.
Type: Function
A function.
Type: object
Type: object
Default: new Map()
The cache storage.
Must implement these methods: has(key)
, set(key, value)
, get(key)
and delete(key)
Type: boolean
Default: true
This flag will force the expiration of the cached value to be computed from the time that value was obtained. If unset, the expiration will be set starting from the request time, without considering the time it takes to get the value into the cache.
Type: number
Default: Infinity
The maximum amount of time in milliseconds to keep a value in the cache.
Expired keys will be deleted from the cache only after the expiration time passes and the key is requested again, forcing a new call to fn
.
Therefore, expiration is not managed by internally setting a timeout per key as done in other similar libraries.
Type: Function
Default: (...args) => args[0]
Determines how the caching key will be computed. By default, it will only consider the first argument and use strict equality to evaluate a match.
The memoized version of fn
.
It resolves to the result of calling fn()
, which is called only once per key and during the time the value has to be cached.
Rejections are not cached.
FAQs
Memoizes a promise-returning function
The npm package promise-mem receives a total of 571 weekly downloads. As such, promise-mem popularity was classified as not popular.
We found that promise-mem demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.