A lightweight, flexible memoization library for JavaScript and TypeScript.
npm install mems
or
yarn add mems
You can use mems
as a default import or named import:
import mems from 'mems'
// or
import { mems } from 'mems'
import mems from 'mems'
const expensiveFunction = (a, b) => {
console.log('Calculating...')
return a + b
}
const memoizedFunction = mems(expensiveFunction)
console.log(memoizedFunction(2, 3)) // Output: Calculating... 5
console.log(memoizedFunction(2, 3)) // Output: 5 (cached result)
import { mems } from 'mems'
const complexFunction = (obj) => {
console.log('Processing...')
return Object.keys(obj).length
}
const memoizedComplexFunction = mems(complexFunction, {
maxCacheSize: 100,
shouldDeepCompareArgs: true
})
console.log(memoizedComplexFunction({ a: 1, b: 2 })) // Output: Processing... 2
console.log(memoizedComplexFunction({ b: 2, a: 1 })) // Output: 2 (cached result)
For convenience, mems
also exports a deepMems
function that uses deep comparison by default:
import { deepMems } from 'mems'
const deepMemoizedFunction = deepMems(complexFunction)
Memoizes the given function.
function
: The function to memoize.options
(optional): An object with the following properties:maxCacheSize
(number, default: 1000): Maximum number of results to cache.shouldDeepCompareArgs
(boolean, default: false): Whether to use deep comparison for arguments.
Returns a memoized version of the function.
Memoizes the given function using deep comparison for arguments.
- Lightweight and fast
- Configurable cache size
- Optional deep comparison of arguments
- TypeScript support
- No dependencies
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.