Memoize
Complete memoize/cache solution for JavaScript
Originally derived from es5-ext package.
Memoization is best technique to save on memory or CPU cycles when we deal with repeated operations. For detailed insight see: http://en.wikipedia.org/wiki/Memoization
Features
Installation
In your project path — note the two e
's in memoizee
:
$ npm install memoizee
memoize
name was already taken, therefore project is published as memoizee
on NPM.
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
Usage
var memoize = require('memoizee');
var fn = function (one, two, three) { };
memoized = memoize(fn);
memoized('foo', 3, 'bar');
memoized('foo', 3, 'bar');
Configuration
All below options can be applied in any combination
Arguments length
By default fixed number of arguments that function take is assumed (it's read from function's length
property) this can be overridden:
memoized = memoize(fn, { length: 2 });
memoized('foo');
memoized('foo', undefined);
memoized('foo', 3, {});
memoized('foo', 3, 13);
Dynamic length behavior can be forced by setting length to false
, that means memoize will work with any number of arguments.
memoized = memoize(fn, { length: false });
memoized('foo');
memoized('foo');
memoized('foo', undefined);
memoized('foo', undefined);
memoized('foo', 3, {});
memoized('foo', 3, 13);
memoized('foo', 3, 13);
Primitive mode
If we work with large result sets, or memoize hot functions, default mode may not perform as fast as we expect. In that case it's good to run memoization in primitive mode. To provide fast access, results are saved in hash instead of an array. Generated hash ids are result of arguments to string convertion. Mind that this mode will work correctly only if stringified arguments produce unique strings.
memoized = memoize(fn, { primitive: true });
memoized('/path/one');
memoized('/path/one');
Resolvers
When we're expecting arguments of certain type it's good to coerce them before doing memoization. We can do that by passing additional resolvers array:
memoized = memoize(fn, { length: 2, resolvers: [String, Boolean] });
memoized(12, [1,2,3].length);
memoized("12", true);
memoized({ toString: function () { return "12"; } }, {});
Note. If your arguments are collections (arrays or hashes) that you want to memoize by content (not by self objects), you need to cast them to strings, for it's best to just use primitive mode. Arrays have standard string representation and work with primitive mode out of a box, for hashes you need to define toString
method, that will produce unique string descriptions, or rely on JSON.stringify
.
Similarly if you want to memoize functions by their code representation not by their objects, you should use primitive mode.
Memoizing asynchronous functions
With async option we indicate that we memoize asynchronous function.
Operations that result with an error are not cached.
afn = function (a, b, cb) {
setTimeout(function () {
cb(null, a + b);
}, 200);
};
memoized = memoize(afn, { async: true });
memoized(3, 7, function (err, res) {
memoized(3, 7, function (err, res) {
});
});
memoized(3, 7, function (err, res) {
});
Memoizing a methods
When we are defining a prototype, we may want to define method that will memoize it's results in relation to each instance. Basic way to obtain that would be:
var Foo = function () {
this.bar = memoize(this.bar.bind(this), { someOption: true });
};
Foo.prototype.bar = function () {
};
There's a lazy methods descriptor generator provided:
var d = require('d');
var memoizeMethods = require('memoizee/methods');
var Foo = function () {
};
Object.definePropeties(Foo.prototype, memoizeMethods({
bar: d(function () {
}, { someOption: true })
}));
Cache handling
Manual clean up:
Delete data for particular call.
memoized.delete('foo', true);
Arguments passed to delete
are treated with same rules as input arguments passed to function
Clear all cached data:
memoized.clear();
Expire cache after given period of time
With maxAge option we can ensure that cache for given call is cleared after predefined period of time (in milliseconds)
memoized = memoize(fn, { maxAge: 1000 });
memoized('foo', 3);
memoized('foo', 3);
setTimeout(function () {
memoized('foo', 3);
memoized('foo', 3);
}, 2000);
Additionally we may ask to pre-fetch in a background a value that is about to expire. Pre-fetch is invoked only if value is accessed close to its expiry date. By default it needs to be within at least 33% of maxAge timespan before expire:
memoized = memoize(fn, { maxAge: 1000, preFetch: true });
memoized('foo', 3);
memoized('foo', 3);
setTimeout(function () {
memoized('foo', 3);
}, 500);
setTimeout(function () {
memoized('foo', 3);
}, 800);
setTimeout(function () {
memoized('foo', 3);
}, 1300);
Pre-fetch timespan can be customized:
memoized = memoize(fn, { maxAge: 1000, preFetch: 0.6 });
memoized('foo', 3);
memoized('foo', 3);
setTimeout(function () {
memoized('foo', 3);
}, 500);
setTimeout(function () {
memoized('foo', 3);
}, 1300);
Thanks @puzrin for helpful suggestions concerning this functionality
Reference counter
We can track number of references returned from cache, and manually delete them. When last reference is cleared, cache is purged automatically:
memoized = memoize(fn, { refCounter: true });
memoized('foo', 3);
memoized('foo', 3);
memoized('foo', 3);
memoized.deleteRef('foo', 3);
memoized.deleteRef('foo', 3);
memoized.deleteRef('foo', 3);
memoized('foo', 3);
Limiting cache size
With max option you can limit cache size, it's backed with LRU algorithm, provided by low-level lru-queue utility
memoized = memoize(fn, { max: 2 });
memoized('foo', 3);
memoized('bar', 7);
memoized('foo', 3);
memoized('bar', 7);
memoized('lorem', 11);
memoized('bar', 7);
memoized('foo', 3);
memoized('lorem', 11);
memoized('foo', 3);
memoized('bar', 7);
Registering dispose callback
You can register callback that is called on each value being removed from cache:
memoized = memoize(fn, { dispose: function (value) { } });
var foo3 = memoized('foo', 3);
var bar7 = memoized('bar', 7);
memoized.clear('foo', 3);
memoized.clear('bar', 7);
Benchmarks
Simple benchmark tests can be found in benchmark folder. Currently it's just plain simple calculation of fibonacci sequences. To run it you need to install other test candidates:
$ npm install underscore lodash lru-cache
Example output taken under Node v0.8.26 on 2008 MBP Pro:
Fibonacci 3000 x10:
1: 25ms Memoizee (primitive mode)
2: 28ms Underscore
3: 34ms lru-cache LRU (max: 1000)
4: 65ms Lo-dash
5: 94ms Memoizee (primitive mode) LRU (max: 1000)
6: 262ms Memoizee (object mode) LRU (max: 1000)
7: 280ms Memoizee (object mode)
Profiling & Statistics
If you want to make sure how much you benefit from memoization or just check if memoization works as expected, loading profile module will give access to all valuable information.
Module needs to be imported before any memoization (that we want to track) is configured. Mind also that running profile module affects performance, it's best not to use it in production environment
var memProfile = require('memoizee/profile');
Access statistics at any time:
memProfile.statistics;
console.log(memProfile.log());
Example console output:
------------------------------------------------------------
Memoize statistics:
Init Cache %Cache Source location
11604 35682 75.46 (all)
2112 19901 90.41 at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:276:12
2108 9087 81.17 at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:293:10
6687 2772 29.31 at /Users/medikoo/Projects/_packages/next/lib/fs/watch.js:125:9
697 3922 84.91 at /Users/medikoo/Projects/_packages/next/lib/fs/is-ignored.js:277:15
------------------------------------------------------------
- Init – Initial hits
- Cache – Cache hits
- %Cache – What's the percentage of cache hits (of all function calls)
- Source location – Where in the source code given memoization was initialized
Tests
$ npm test
Contributors
- @puzrin (Vitaly Puzrin)
- Proposal and help with coining right pre-fetch logic for maxAge variant