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 = const fn = { /* ... */ } const memoized = // Cache hit
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 =
The custom cache should be an object containing a create
method that returns
an object implementing the following methods:
get
set
has
Custom serializer
To use a custom serializer:
const memoized =
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.gitcd fast-memoizenpm installnpm 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).
// Rest parameter example { return theArgs}multiplylength // => 1 // Default parameter example { return divisor * element}dividelength // => 1
So if you use rest & default parameters, explicitly set the strategy to variadic.
const memoizedMultiply =
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 { if !x__memoizedId x__memoizedId = ++id return __memoizedId: x__memoizedId }
Credits
- Icon by Mary Rankin from the Noun Project
- Bullet train ZSH theme
caiogondim.com · GitHub @caiogondim · Twitter @caio_gondim