
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
memoize-immutable
Advanced tools
An efficient memoizer for functions that only receive immutable arguments. Ideal for Redux and similar environments.
An efficient memoizer for functions that only receive immutable arguments. Ideal for Redux and similar environments.
This lib is only compatible with browsers that implement WeakMap
and Map
natively.
(which have good browser support).
In order to index cached results, most memoizers serialize arguments using JSON.stringify
or similar methods.
When working with immutable data, using a WeakMap based cache is much more CPU and memory efficient.
This memoizer is designed to work with such caches.
npm install --save memoize-immutable
memoize( fn [, options ] )
fn
: the function to memoizeoptions
(optionnal):
cache
: a cache instance implementing .has
, .get
and .set
methods (defaults to TupleMap)limit
: limit the size of the default cache (incompatible with cache
option)return
s a memoized function.
Note: the .displayName
of the returned function will be '<original name>Memoized'
.
var memoize = require('memoize-immutable');
var nbExecs = 0;
var arraySum = function(arr) {
nbExecs++;
return arr.reduce(function(acc, curr) {
return acc + curr;
}, 0);
};
var arraySumMemoized = memoize(arraySum);
var arr1 = [ 1, 2, 3, 4, 5, 6 ];
var copy = arr1;
expect(arraySumMemoized(arr1)).to.equal(21);
expect(nbExecs).to.equal(1);
expect(arraySumMemoized(copy)).to.equal(21);
expect(nbExecs).to.equal(1);
// Of course, you shouldn't mutate the arguments, or else...
arr1.push(7);
expect(arraySumMemoized(arr1)).to.equal(21);
expect(nbExecs).to.equal(1);
var clone = arr1.concat();
expect(arraySumMemoized(clone)).to.equal(28);
expect(nbExecs).to.equal(2);
## Choosing a cache store
NB: When in doubt, don't use an optional cache.
The following instructions will help choose optimal cache store for a given function. Before you proceed, make sure you know the definition of the following terms:
number
, string
, boolean
, undefined
or null
value is considered primitive.object
, array
or function
value is non-primitive.drawRect(20, 50, 100, 150, '#000');
and the same function, accepting named arguments:
drawRect({x: 20, y: 50, width: 100, height: 150, color: '#000'});
which is expected to have the exact same result as:
drawRect({color: '#000', width: 100, height: 150, x: 20, y: 50});
MPL-2.0
FAQs
An efficient memoizer for functions that only receive immutable arguments. Ideal for Redux and similar environments.
The npm package memoize-immutable receives a total of 2,221 weekly downloads. As such, memoize-immutable popularity was classified as popular.
We found that memoize-immutable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.