Skip to content

Commit 9f79d04

Browse files
authored
Merge pull request #3 from davidchin/more_readme
CHECKOUT-4272: Add more information to README
2 parents 445a8cf + ed7cfef commit 9f79d04

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff 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
2424
function fn(a, b) {
@@ -32,6 +32,37 @@ const result2 = memoizedFn({ message: 'hello' }, { message: 'world' });
3232
expect(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

0 commit comments

Comments
 (0)