1.0.0 • Published 2 years ago

@unsudo/memoizer v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

memoizer

Store the results of expensive function calls and return the cached result when the same input occurs again. A specific expiration time can be set for that result. Supports both synchronous and asynchronous functions.

Synchronous usage

const { memoizeSync } = require('@unsudo/memoizer.js');

const expensiveSyncFn = (x, y) => {
    for (let i = 0; i < 1e10; i++);
    return x + y;
};

// Custom unique key generation  to store the result under it
const uniqueKey = (x, y) => `${x}-${y}`;

const fnSync = memoizeSync(
    expensiveSyncFn,
    uniqueKey
);

// Takes some time to print
console.log(fnSync(3, 4));
// Prints instantly
console.log(fnSync(3, 4));

// Will return null if invoked after 1h
const fnSyncWithExpire = memoizeSync(
    expensiveSyncFn,
    uniqueKey,
    { ttl: 60*60*1000 }
);

// Will return 'Expired' if invoked after 1h
const fnSyncWithExpireCb = memoizeSync(
    expensiveSyncFn,
    uniqueKey,
    { ttl: 60*60*1000, onExpire: () => 'Expired' }
);

Asynchronous usage

const { memoizeAsync } = require('@unsudo/memoizer.js');

const expensiveAsyncFn = (x, y) => new Promise((resolve, reject) => {
    setTimeout(() => resolve(x + y), 3000);
});

// Custom unique key generation  to store the result under it
const uniqueKey = (x, y) => `${x}-${y}`;

const fnAsync = memoizeAsync(
    expensiveAsyncFn,
    uniqueKey
);

// Prints after 3000ms
console.log(await fnAsync(3, 4));
// Prints instantly
console.log(await fnAsync(3, 4));

// Will return null if invoked after 1h
const fnAsyncWithExpire = memoizeAsync(
    expensiveAsyncFn,
    uniqueKey,
    { ttl: 60*60*1000 }
);

// Will return 'Expired' if invoked after 1h
const fnAsyncWithExpireCb = memoizeAsync(
    expensiveAsyncFn,
    uniqueKey,
    { ttl: 60*60*1000, onExpire: () => 'Expired' }
);

Installation

npm install @unsudo/memoizer