What is fast-memoize?
The fast-memoize npm package is a high-performance memoization library that caches the results of function calls, so that the result can be quickly retrieved when the function is called again with the same arguments. This can significantly improve performance for functions with expensive computations or I/O operations that are called repeatedly with the same inputs.
What are fast-memoize's main functionalities?
Simple memoization
This feature allows you to memoize a simple function like the Fibonacci sequence. The memoized version of the function will remember the results of previous calls and return the cached result when the same input occurs again.
const memoize = require('fast-memoize');
const fibonacci = n => (n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2));
const memoizedFibonacci = memoize(fibonacci);
console.log(memoizedFibonacci(10));
Custom cache
This feature allows you to provide a custom cache implementation. By default, fast-memoize uses a plain JavaScript object as a cache, but you can replace it with any other object that follows the Map interface, such as a Map instance.
const memoize = require('fast-memoize');
const myCache = new Map();
const memoizedFn = memoize(fn, { cache: { create: () => myCache } });
Custom serializer
This feature allows you to provide a custom serializer for the arguments of the memoized function. By default, fast-memoize uses a serializer that can handle primitives and object references, but with a custom serializer, you can extend this to handle more complex serialization scenarios.
const memoize = require('fast-memoize');
const jsonSerializer = { serialize: JSON.stringify };
const memoizedFn = memoize(fn, { serializer: jsonSerializer });
Custom strategy
This feature allows you to provide a custom memoization strategy. fast-memoize comes with a default strategy that works well for most cases, but you can implement your own strategy for more control over how memoization is applied.
const memoize = require('fast-memoize');
const customStrategy = require('my-custom-strategy');
const memoizedFn = memoize(fn, { strategy: customStrategy });
Other packages similar to fast-memoize
lodash.memoize
lodash.memoize is a memoization function from the popular Lodash utility library. It is similar to fast-memoize but may not be as performance-focused. Lodash allows for custom cache implementations but is generally considered to be larger and more feature-rich than fast-memoize.
memoizee
memoizee is another npm package that offers memoization. It provides a rich set of features, including the ability to limit cache size, expire cached items, and primitive mode for arguments handling. It is more configurable than fast-memoize but might be slower due to its additional features.
mem
mem is a minimalist memoization library that can cache the results of functions with any type of arguments, not just primitives. It offers TTL (time-to-live) for cache entries and is designed to be simple and efficient, though it may not be as fast as fast-memoize for certain use cases.
fast-memoize
In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
— Wikipedia
This library is an attempt to make the fastest possible memoization library in
JavaScript that supports N arguments.
Installation
npm install fast-memoize --save
Usage
const memoize = require('fast-memoize')
const fn = function (one, two, three) { }
const memoized = memoize(fn)
memoized('foo', 3, 'bar')
memoized('foo', 3, 'bar')
Custom cache
The fastest cache is used for the running environment, but it is possible to
pass a custom cache to be used.
const memoized = memoize(fn, {
cache: {
create() {
var store = {};
return {
has(key) { return (key in store); },
get(key) { return store[key]; },
set(key, value) { store[key] = value; }
};
}
}
})
The custom cache should be an object containing a create
method that returns
an object implementing the following methods:
Custom serializer
To use a custom serializer:
const memoized = memoize(fn, {
serializer: customSerializer
})
The serializer is a function that receives one argument and outputs a string
that represents it. It has to be a
deterministic algorithm
meaning that, given one input, it always returns the same output.
Benchmark
For an in depth explanation on how this library was created, go read
this post on RisingStack.
Below you can see a performance benchmark between some of the most popular libraries
for memoization.
To run the benchmark, clone the repo, install the dependencies and run npm run benchmark
.
git clone git@github.com:caiogondim/fast-memoize.git
cd fast-memoize
npm install
npm run benchmark
Against another git hash
To benchmark the current code against a git hash, branch, ...
npm run benchmark:compare 53fa9a62214e816cf8b5b4fa291c38f1d63677b9
Gotchas
Rest & Default Parameters
We check for function.length
to get upfront the expected number of arguments
in order to use the fastest strategy. But when rest & default parameters are being used, we don't receive the right number of arguments (see details).
function multiply (multiplier, ...theArgs) {
return theArgs.map(function (element) {
return multiplier * element
})
}
multiply.length
function divide (element, divisor = 1) {
return divisor * element
}
divide.length
So if you use rest & default parameters, explicitly set the strategy to variadic.
const memoizedMultiply = memoize(multiply, {
strategy: memoize.strategies.variadic
})
Function Arguments
The default serializer uses JSON.stringify
which will serialize functions as
null
. This means that if you are passing any functions as arguments you will
get the same output regardless of whether you pass in different functions or
indeed no function at all. The cache key generated will always be the same. To
get around this you can give each function a unique ID and use that.
let id = 0
function memoizedId(x) {
if (!x.__memoizedId) x.__memoizedId = ++id
return { __memoizedId: x.__memoizedId }
}
memoize((aFunction, foo) => {
return aFunction.bind(foo)
}, {
serializer: args => {
const argumentsWithFuncIds = Array.from(args).map(x => {
if (typeof x === 'function') return memoizedId(x)
return x
})
return JSON.stringify(argumentsWithFuncIds)
}
})
Credits
caiogondim.com ·
GitHub @caiogondim ·
Twitter @caio_gondim