1.0.0 • Published 3 years ago
easy-memoizer v1.0.0
easy-memoizer
Store the results of expensive function calls and return the cached result when the same inputs occur again. Supports both synchronous and asynchronous functions.
Synchronous usage
const { memoizeSync } = require('../memoizer.js');
const expensiveSyncFn = (x, y) => {
for (let i = 0; i < 1e10; i++);
return x + y;
};
const fnSync = memoizeSync(expensiveSyncFn, (x, y) => `${x}-${y}`);
// It will take some time to print
console.log(fnSync(3, 4));
// Prints instantly
console.log(fnSync(3, 4));
Asynchronous usage
const { memoizeAsync } = require('../memoizer.js');
const expensiveAsyncFn = (x, y) => new Promise((resolve, reject) => {
setTimeout(() => resolve(x + y), 3000);
});
const fnAsync = memoizeAsync(expensiveAsyncFn, (x, y) => `${x}-${y}`);
// Printing after 3000ms
console.log(await fnAsync(3, 4));
// Prints instantly
console.log(await fnAsync(3, 4));
Installation
npm install easy-memoizer
1.0.0
3 years ago