timed-memoize
Timed memoize.
Basic usage
; // Memoize function return valuesconst memoizedFunction = ;; // returns myFucntion('foo'), saves the value in memory for later use // Memoize using key/value pairsconst memory = ;; // sets 'key' to 'value'; // gets the value
Tiny library that wraps a function to memoize its return values when given specific arguments. Return values are cached on a per-arguments basis. This means that if you call the function with different arguments, then the cache will be missed and a new value will be computed with those arguments (see examples below). Optionally you can specify how to resolve the arguments to a key that is used internally to cache return values.
It is also possible to memoize key/value pairs using a cache.
Works in browsers and Node.js
Options
Memoizing functions
fn
(required): Function whose return values to memoize.options
(default:{}
): An object with the following options:timeout
(default:0
): The amount of time in milliseconds to keep the function's return value in cache for. Keeps values indefinitely fortimeout = -1
.hot
(default:true
): If enabled, keeps track of when the last call to the function was made (with the arguments supplied); it then makes sure the cache is kept for an additionaltimeout
amount of time. This helps keeping the cache alive for frequently used values.resolver
(default:args => args
): A function that accepts a single array and transforms it into a key. The returned key can be anything that can be used as a key in standard JavaScript objects. Not used whenone = true
.discardUndefined
(default:false
): If the underlying function returnsundefined
, then don't cache the value and re-evaluate the function on the next call.one
(default:false
) Only ever remember one value. When the memoized function is repeatedly called with the same arguments, the cache will hit and the underlying function is not called. When the memoized function is called with different arguments, the cache will miss and a new value is cached by calling the underlying function. Note: the arguments are checked shallowly for equality.
Returns a function that can be invoked like the underlying function, but returns cached results.
Memoizing key/value pairs
options
(default:{}
): An object with the following options:timeout
(default:0
): The amount of time in milliseconds to keep the key/value pair in cache for. Keeps values indefinitely fortimeout = -1
.hot
(default:true
): If enabled, keeps track of when the last request for the value was made; it then makes sure the cache is kept for an additionaltimeout
amount of time. This helps keeping the cache alive for frequently used values.
Returns a function that can be used to set key/value pairs, and can return the cached values.
const memory = ;
is equivalent to
const memory = ;
Examples
; { console return Math;} // Caches immediately, even when called synchronously.const myHeavyComputationMemoized = ;console; // 1, cache missconsole; // 2, cache missconsole; // 1, cache hit // Cold cache.// Any values returned by the underlying function are forgotten after 500ms.; // 3, cache miss; // 3, cache hit; // 3, cache miss // Hot cache.// Any values returned by the underlying function are forgotten after 500ms.// If the function is called again with the same arguments, the timer is reset. const myHotAndHeavyComputation = ;; // 4, cache miss; // 4, cache hit; // 4, cache hit; // 4, cache miss // Cache with custom resolver.// Resolver is used to remember return values from past calls. { console; return x + 1;}const mySideEffectWithUnimportantArguments = ;console; // 2, cache missconsole; // 2, cache hit // Storing and retrieving key/value pairsconst memory = ;;console; // bar; // bar; // undefined // Set timeout to -1, keep this value for ever.const memoizeForever = ;; // 4, cache miss; // 4, cache hit; // 4, cache hit // Only ever remember one value, change value based on function arguments.const memoizeOne = ;; // 4, cache miss; // 4, cache hit; // 4, cache hit; // 5, cache miss; // 5, cache hit; // 4, cache miss; // 4, cache hit
Installation
Node
npm install --save timed-memoize