Triemoize
Efficient, reliable and memory leak free memoization using WeakMaps and tries
There are many memoization libraries out there. What this library tries to achieve is to be
- Efficient
- Reliable
- Memory leak free
For all this to work, you must always use immutable data as function arguments when you're calling the memoized function.
Installation
npm install --save triemoize
If you're using Yarn
yarn add triemoize
Usage
// Create a pure functionconst add = a b = a + b // Memoize itconst memoizedAdd = // The function was called // This time the result was taken from cache
Unlike from other memoization techniques like serialization, you can safely use huge objects or arrays as function arguments without worrying about performance or memory leaks. However, all the arguments you pass in must be immutable. Otherwise the memoization would not work correctly.
const getStringsLessThan = const food = 'milk' 'bread' 'cheese' 'chocolate' 'potatoes' 'bacon' 'butter' 'eggs' 'sausages' 'pork' 'yoghurt' 'apple' // The function was called // The result was taken from cache
Named arguments
Named arguments (using ES6 destructuring) are not supported in the moment. I plan to add it later.
// Not supported in the moment const add = a b = a + bconst memoizedAdd = // The function was called // Fail, the value was recomputed
How it works
TODO
Benchmark
TODO
License
MIT