- Notifications
You must be signed in to change notification settings - Fork3
A JavaScript library for memoizing the result of a pure function
License
bigcommerce/memoize-js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This library can be used to memoize the result of a pure function.
Unlike the defaultmemoize function provided by Lodash, it can be applied to functions that accept multiple non-primitive arguments. It can also be configured to expire its cache after certain number of unique calls. By default, it compares object-based arguments shallowly; but it can be configured to compare arguments strictly or deeply depending on your usage requirement.
You can install this library usingnpm.
npm install --save @bigcommerce/memoize
To memoize a function:
functionfn(a,b){return{ a, b};}constmemoizedFn=memoize(fn);constresult=memoizedFn({message:'hello'},{message:'world'});constresult2=memoizedFn({message:'hello'},{message:'world'});expect(result).toBe(result2);
To set a limit on the cache size:
functionfn(a,b){return{ a, b};}constmemoizedFn=memoize(fn,{maxSize:1});constresult=memoizedFn({message:'hello'},{message:'world'});// This call will expire the cache of the previous call because it is called with a different set of argumentsconstresult2=memoizedFn({message:'hello'},{message:'foobar'});constresult3=memoizedFn({message:'hello'},{message:'world'});expect(result3).not.toBe(result);
There is a convenience method for setting the cache size to one:
constmemoizedFn=memoizeOne(fn);
To use a different argument comparison function:
constmemoizedFn=memoize(fn,{isEqual:(a,b)=>a===b,});
To release:
npm run release
To see other available commands:
npm run
MIT
About
A JavaScript library for memoizing the result of a pure function
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.