https://github.com/imcuttle/memoize-fn
A memoization library that caches the result of the different arguments
https://github.com/imcuttle/memoize-fn
Last synced: 4 months ago
JSON representation
A memoization library that caches the result of the different arguments
- Host: GitHub
- URL: https://github.com/imcuttle/memoize-fn
- Owner: imcuttle
- License: mit
- Created: 2019-04-30T08:17:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-09-21T11:02:34.000Z (over 2 years ago)
- Last Synced: 2025-08-17T03:08:12.148Z (10 months ago)
- Language: JavaScript
- Size: 1.99 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: License
Awesome Lists containing this project
README
# memoize-fn
[](https://travis-ci.org/imcuttle/memoize-fn)
[](https://codecov.io/github/imcuttle/memoize-fn?branch=master)
[](https://www.npmjs.com/package/memoize-fn)
[](https://www.npmjs.com/package/memoize-fn)
[](https://prettier.io/)
[](https://conventionalcommits.org)
> A memoization library that caches the result of the different arguments
## Installation
```bash
npm install memoize-fn
# or use yarn
yarn add memoize-fn
```
## Usage
```javascript
import memoizeFn from 'memoize-fn'
import { withCtx, robust } from 'memoize-fn'
let count = 1
const fn = memoizeFn(() => count++)
fn() // => 1
fn() // => 1
fn('new argument') // => 2
fn() // => 1
count // => 3
```
## API
### memoize
[index.js:15-50](https://github.com/imcuttle/memoize-fn/blob/dc408a341ecd88fa2eda280c263fca95440fe212/index.js#L15-L50 'Source code on GitHub')
Memoize function that caches the result of the different arguments.
#### Parameters
- `fn` {Function}
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** {MemoizeOptions} (optional, default `{}`)
- `options.once` (optional, default `false`)
- `options.eq` (optional, default `(prevArgs,newArgs)=>shallowEqual(prevArgs,newArgs)`)
- `options.cache` (optional, default `new Map()`)
- `options.skipEqualThis` (optional, default `true`)
Returns **any** memoizeFn {Function}
### withCtx
[index.js:59-78](https://github.com/imcuttle/memoize-fn/blob/dc408a341ecd88fa2eda280c263fca95440fe212/index.js#L59-L78 'Source code on GitHub')
Memoize function that caches the result of the different arguments and with context
#### Parameters
- `fn` {Function}
- `opts` {MemoizeOptions}
Returns **[CtxFunction](#ctxfunction)**
### robust
[index.js:87-102](https://github.com/imcuttle/memoize-fn/blob/dc408a341ecd88fa2eda280c263fca95440fe212/index.js#L87-L102 'Source code on GitHub')
Memoize function that caches the result of the different arguments and resets memoize function when catches error asynchronously.
#### Parameters
- `fn` {Function}
- `opts` {MemoizeOptions}
Returns **[CtxFunction](#ctxfunction)**
### MemoizeOptions
[index.js:87-102](https://github.com/imcuttle/memoize-fn/blob/dc408a341ecd88fa2eda280c263fca95440fe212/index.js#L87-L102 'Source code on GitHub')
Type: {}
#### Parameters
- `once` {boolean} - Only cache once like [memoize-one](https://github.com/alexreardon/memoize-one) (optional, default `false`)
- `eq` {(prevArgs, newArgs) => boolean} (optional, default `shallowEqual`)
- `cache` {Map} (optional, default `newMap()`)
- `skipEqualThis` {boolean} (optional, default `true`)
### CtxFunction
[index.js:87-102](https://github.com/imcuttle/memoize-fn/blob/dc408a341ecd88fa2eda280c263fca95440fe212/index.js#L87-L102 'Source code on GitHub')
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)
#### Parameters
- `reset` {Function} - Resets cache
- `unCache` {Function} - Disables cache
## Contributing
- Fork it!
- Create your new branch:
`git checkout -b feature-new` or `git checkout -b fix-which-bug`
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
`git commit -am 'feat: some description (close #123)'` or `git commit -am 'fix: some description (fix #123)'`
- Push to the branch: `git push`
- Submit a pull request :)
## Authors
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
## License
MIT - [imcuttle](https://github.com/imcuttle) 🐟