1 |
3a515b92
|
cagy
|
# mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem)
|
2 |
|
|
|
3 |
|
|
> [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
|
4 |
|
|
|
5 |
|
|
Memory is automatically released when an item expires.
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
## Install
|
9 |
|
|
|
10 |
|
|
```
|
11 |
|
|
$ npm install mem
|
12 |
|
|
```
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
## Usage
|
16 |
|
|
|
17 |
|
|
```js
|
18 |
|
|
const mem = require('mem');
|
19 |
|
|
|
20 |
|
|
let i = 0;
|
21 |
|
|
const counter = () => ++i;
|
22 |
|
|
const memoized = mem(counter);
|
23 |
|
|
|
24 |
|
|
memoized('foo');
|
25 |
|
|
//=> 1
|
26 |
|
|
|
27 |
|
|
// Cached as it's the same arguments
|
28 |
|
|
memoized('foo');
|
29 |
|
|
//=> 1
|
30 |
|
|
|
31 |
|
|
// Not cached anymore as the arguments changed
|
32 |
|
|
memoized('bar');
|
33 |
|
|
//=> 2
|
34 |
|
|
|
35 |
|
|
memoized('bar');
|
36 |
|
|
//=> 2
|
37 |
|
|
```
|
38 |
|
|
|
39 |
|
|
##### Works fine with promise returning functions
|
40 |
|
|
|
41 |
|
|
```js
|
42 |
|
|
const mem = require('mem');
|
43 |
|
|
|
44 |
|
|
let i = 0;
|
45 |
|
|
const counter = async () => ++i;
|
46 |
|
|
const memoized = mem(counter);
|
47 |
|
|
|
48 |
|
|
(async () => {
|
49 |
|
|
console.log(await memoized());
|
50 |
|
|
//=> 1
|
51 |
|
|
|
52 |
|
|
// The return value didn't increase as it's cached
|
53 |
|
|
console.log(await memoized());
|
54 |
|
|
//=> 1
|
55 |
|
|
})();
|
56 |
|
|
```
|
57 |
|
|
|
58 |
|
|
```js
|
59 |
|
|
const mem = require('mem');
|
60 |
|
|
const got = require('got');
|
61 |
|
|
const delay = require('delay');
|
62 |
|
|
|
63 |
|
|
const memGot = mem(got, {maxAge: 1000});
|
64 |
|
|
|
65 |
|
|
(async () => {
|
66 |
|
|
await memGot('sindresorhus.com');
|
67 |
|
|
|
68 |
|
|
// This call is cached
|
69 |
|
|
await memGot('sindresorhus.com');
|
70 |
|
|
|
71 |
|
|
await delay(2000);
|
72 |
|
|
|
73 |
|
|
// This call is not cached as the cache has expired
|
74 |
|
|
await memGot('sindresorhus.com');
|
75 |
|
|
})();
|
76 |
|
|
```
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
## API
|
80 |
|
|
|
81 |
|
|
### mem(fn, [options])
|
82 |
|
|
|
83 |
|
|
#### fn
|
84 |
|
|
|
85 |
|
|
Type: `Function`
|
86 |
|
|
|
87 |
|
|
Function to be memoized.
|
88 |
|
|
|
89 |
|
|
#### options
|
90 |
|
|
|
91 |
|
|
Type: `Object`
|
92 |
|
|
|
93 |
|
|
##### maxAge
|
94 |
|
|
|
95 |
|
|
Type: `number`<br>
|
96 |
|
|
Default: `Infinity`
|
97 |
|
|
|
98 |
|
|
Milliseconds until the cache expires.
|
99 |
|
|
|
100 |
|
|
##### cacheKey
|
101 |
|
|
|
102 |
|
|
Type: `Function`
|
103 |
|
|
|
104 |
|
|
Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
|
105 |
|
|
|
106 |
|
|
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
|
107 |
|
|
|
108 |
|
|
##### cache
|
109 |
|
|
|
110 |
|
|
Type: `Object`<br>
|
111 |
|
|
Default: `new Map()`
|
112 |
|
|
|
113 |
|
|
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.
|
114 |
|
|
|
115 |
|
|
##### cachePromiseRejection
|
116 |
|
|
|
117 |
|
|
Type: `boolean`<br>
|
118 |
|
|
Default: `false`
|
119 |
|
|
|
120 |
|
|
Cache rejected promises.
|
121 |
|
|
|
122 |
|
|
### mem.clear(fn)
|
123 |
|
|
|
124 |
|
|
Clear all cached data of a memoized function.
|
125 |
|
|
|
126 |
|
|
#### fn
|
127 |
|
|
|
128 |
|
|
Type: `Function`
|
129 |
|
|
|
130 |
|
|
Memoized function.
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
## Tips
|
134 |
|
|
|
135 |
|
|
### Cache statistics
|
136 |
|
|
|
137 |
|
|
If you want to know how many times your cache had a hit or a miss, you can make use of [stats-map](https://github.com/SamVerschueren/stats-map) as a replacement for the default cache.
|
138 |
|
|
|
139 |
|
|
#### Example
|
140 |
|
|
|
141 |
|
|
```js
|
142 |
|
|
const mem = require('mem');
|
143 |
|
|
const StatsMap = require('stats-map');
|
144 |
|
|
const got = require('got');
|
145 |
|
|
|
146 |
|
|
const cache = new StatsMap();
|
147 |
|
|
const memGot = mem(got, {cache});
|
148 |
|
|
|
149 |
|
|
(async () => {
|
150 |
|
|
await memGot('sindresorhus.com');
|
151 |
|
|
await memGot('sindresorhus.com');
|
152 |
|
|
await memGot('sindresorhus.com');
|
153 |
|
|
|
154 |
|
|
console.log(cache.stats);
|
155 |
|
|
//=> {hits: 2, misses: 1}
|
156 |
|
|
})();
|
157 |
|
|
```
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
## Related
|
161 |
|
|
|
162 |
|
|
- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
## License
|
166 |
|
|
|
167 |
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|