You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.3 KiB

4 years ago
  1. # mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem)
  2. > [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
  3. Memory is automatically released when an item expires.
  4. ## Install
  5. ```
  6. $ npm install mem
  7. ```
  8. ## Usage
  9. ```js
  10. const mem = require('mem');
  11. let i = 0;
  12. const counter = () => ++i;
  13. const memoized = mem(counter);
  14. memoized('foo');
  15. //=> 1
  16. // Cached as it's the same arguments
  17. memoized('foo');
  18. //=> 1
  19. // Not cached anymore as the arguments changed
  20. memoized('bar');
  21. //=> 2
  22. memoized('bar');
  23. //=> 2
  24. ```
  25. ##### Works fine with promise returning functions
  26. ```js
  27. const mem = require('mem');
  28. let i = 0;
  29. const counter = async () => ++i;
  30. const memoized = mem(counter);
  31. (async () => {
  32. console.log(await memoized());
  33. //=> 1
  34. // The return value didn't increase as it's cached
  35. console.log(await memoized());
  36. //=> 1
  37. })();
  38. ```
  39. ```js
  40. const mem = require('mem');
  41. const got = require('got');
  42. const delay = require('delay');
  43. const memGot = mem(got, {maxAge: 1000});
  44. (async () => {
  45. await memGot('sindresorhus.com');
  46. // This call is cached
  47. await memGot('sindresorhus.com');
  48. await delay(2000);
  49. // This call is not cached as the cache has expired
  50. await memGot('sindresorhus.com');
  51. })();
  52. ```
  53. ## API
  54. ### mem(fn, [options])
  55. #### fn
  56. Type: `Function`
  57. Function to be memoized.
  58. #### options
  59. Type: `Object`
  60. ##### maxAge
  61. Type: `number`<br>
  62. Default: `Infinity`
  63. Milliseconds until the cache expires.
  64. ##### cacheKey
  65. Type: `Function`
  66. 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.
  67. You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
  68. ##### cache
  69. Type: `Object`<br>
  70. Default: `new Map()`
  71. 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.
  72. ##### cachePromiseRejection
  73. Type: `boolean`<br>
  74. Default: `false`
  75. Cache rejected promises.
  76. ### mem.clear(fn)
  77. Clear all cached data of a memoized function.
  78. #### fn
  79. Type: `Function`
  80. Memoized function.
  81. ## Tips
  82. ### Cache statistics
  83. 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.
  84. #### Example
  85. ```js
  86. const mem = require('mem');
  87. const StatsMap = require('stats-map');
  88. const got = require('got');
  89. const cache = new StatsMap();
  90. const memGot = mem(got, {cache});
  91. (async () => {
  92. await memGot('sindresorhus.com');
  93. await memGot('sindresorhus.com');
  94. await memGot('sindresorhus.com');
  95. console.log(cache.stats);
  96. //=> {hits: 2, misses: 1}
  97. })();
  98. ```
  99. ## Related
  100. - [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
  101. ## License
  102. MIT © [Sindre Sorhus](https://sindresorhus.com)