Helper for memoizing synchronous functions and methods
Yet another memoizer for synchronous functions.
var memoizeSync = require'memoizesync';// ...return result;var memoized = memoizeSyncmyExpensiveComputation;
memoized works exactly like
myExpensiveComputation, except that
the actual computation is only performed once for each unique set of
var result = memoized42 100;// Got the result!var result2 = memoized42 100;// Got the same result, and much faster this time!
The function returned by
memoizeSync invokes the wrapped function
in the context it's called in itself, so
memoizeSync even works for
memoizing a method that has access to instance variables:
thisname = name;thismyMethod = memoizeSyncconsole.log"Cool, this.name works here!" thisname;// ...return "That was tough, but I'm done now!";;
Foo.prototype.myMethod = memoizeSync(...)
wouldn't work as the memoizer would be shared among all instances of
To distinguish different invocations (whose results need to be cached
memoizeSync relies on a naive stringification of the
arguments, which is looked up in an internally kept hash. If the
function you're memoizing takes non-primitive arguments you might want
to provide a custom
argumentsStringifier as the second argument to
memoizeSync. Otherwise all object arguments will be considered equal
because they stringify to
var memoized = memoizeSync// ...return Objectkeysobjjoin'';return argsmap return JSONstringifyarg;join",";;memoizedfoo: 'bar'; // 'foo'memoizedquux: 'baz'; // 'quux'
Had the custom
argumentsStringifier not been provided, the memoized
function would would have returned
foo both times.
Check out the custom argumentsStringifier test for another example.
You can forcefully clear a specific memoized value using the
method on the memoizer:
var memoized = memoizeSync// ...return theResult;;var foo = memoized123;memoizedpurge123;foo = memoized123; // Will be recomputed
memoizer.purgeAll() clears all memoized results.
You can also specify a custom ttl (in milliseconds) on the memoized results:
var memoized = memoizeSync// ...return theResult;maxAge: 1000;
In the above example the memoized value will be considered stale one
second after it has been computed, and it will be recomputed next time
memoizeSync is invoked with the same arguments.
memoizeSync uses node-lru-cache to
store the memoized values, and it accepts the same parameters in the
If you want to use the
length option for lru-cache, note that the
memoized values are arrays:
var memoizedFsReadFileSync = memoizeAsyncrequire'fs'readFileSyncmax: 1000000if exceptionAndReturnValue0return 1;elsevar body = exceptionAndReturnValue1;return BufferisBufferbody ? bodylength : BufferbyteLengthbody;maxAge: 1000;
The LRU instance is exposed in the
cache property of the memoized
function in case you need to access it.
If a memoized function throws an error, memoizeSync will catch and rethrow it,
so memoizeSync is transparent in that regard. By default, exceptions won't be
saved in the cache, so the original function will be run again on the next
invocation of the memoized function. If you want exceptions to be memoized as
well, set the
exceptions option to
Make sure you have node.js and npm installed, then run:
npm install memoizesync
memoizeSync uses the UMD wrapper, so it should also work in
browsers. You should also have the node-lru-cache
Object.defineProperty and doesn't include an UMD
wrapper, but if you define a
shims config it should be possible to
get it memoizeSync working with require.js, at least in newer browsers.
3-clause BSD license -- see the
LICENSE file for details.