File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed
Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ npm install --save @bigcommerce/memoize
1818
1919## Usage
2020
21- To memoize a function
21+ To memoize a function:
2222
2323``` ts
2424function fn(a , b ) {
@@ -32,6 +32,37 @@ const result2 = memoizedFn({ message: 'hello' }, { message: 'world' });
3232expect (result ).toBe (result2 );
3333```
3434
35+ To set a limit on the cache size:
36+
37+ ``` ts
38+ function fn(a , b ) {
39+ return { a , b };
40+ }
41+
42+ const memoizedFn = memoize (fn , { maxSize: 1 });
43+ const result = memoizedFn ({ message: ' hello' }, { message: ' world' });
44+
45+ // This call will expire the cache of the previous call because it is called with a different set of arguments
46+ const result2 = memoizedFn ({ message: ' hello' }, { message: ' foobar' });
47+ const result3 = memoizedFn ({ message: ' hello' }, { message: ' world' });
48+
49+ expect (result3 ).not .toBe (result );
50+ ```
51+
52+ There is a convenience method for setting the cache size to one:
53+
54+ ``` ts
55+ const memoizedFn = memoizeOne (fn );
56+ ```
57+
58+ To use a different argument comparison function:
59+
60+ ``` ts
61+ const memoizedFn = memoize (fn , {
62+ isEqual : (a , b ) => a === b ,
63+ });
64+ ```
65+
3566
3667## Contribution
3768
You can’t perform that action at this time.
0 commit comments