rememor
v0.0.4
Published
memoizing function wrapper
Downloads
4
Readme
Rememor
Simple memoization module, to speed up recursion, and calls to expensive functions.
Installation
$ npm install rememor
Usage
const remem = require('rememor');
//remem.doc(true) prints out a little info on the module.
let add = (a,b) => a + b;
const memAdd = remem.shallowRemem(add);
memAdd(1,2);
=> 3
memAdd(1,2);
=> 3 //But uses cache instead of calculating.
let fib = (n) => {
if(n !== 0 && n !== 1) {
return fib(n-2) + fib(n-1);
} else {
return n;
}
}
let memFib = remem.deepRemem(fib, 'fib');
//First argument is recursive function, second argument is the name
//of the recursive function to be replaced with the original function definition.
memFib(300);
=> uses caches within function and returns in linear time.
let kindaLog = remem.kindaRemem(Math.log, null, .9);
//This is an approximation using memoization
kindaLog(.1000000007);
=> //Calculates using original function
kindaLog(.1000000006);
=> //Returns the cached value for the previous call because similar args.
Warning:
Be careful when using deepRemem, it modifies the function.