Comparing version 5.1.1 to 6.0.0
@@ -23,21 +23,35 @@ declare namespace mem { | ||
/** | ||
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 (if it's a `function`, its reference will be used as key), otherwise it's all the function arguments JSON stringified as an array. | ||
Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). | ||
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. | ||
A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option). | ||
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible: | ||
``` | ||
import mem = require('mem'); | ||
mem(function_, {cacheKey: JSON.stringify}); | ||
``` | ||
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on. | ||
``` | ||
import mem = require('mem'); | ||
import serializeJavascript = require('serialize-javascript'); | ||
mem(function_, {cacheKey: serializeJavascript}); | ||
``` | ||
@default arguments_ => arguments_[0] | ||
@example arguments_ => JSON.stringify(arguments_) | ||
*/ | ||
readonly cacheKey?: (...arguments: ArgumentsType) => CacheKeyType; | ||
readonly cacheKey?: (arguments: ArgumentsType) => CacheKeyType; | ||
/** | ||
Use a different cache storage. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. | ||
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. | ||
@default new Map() | ||
@example new WeakMap() | ||
*/ | ||
readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>; | ||
/** | ||
Cache rejected promises. | ||
@default true | ||
*/ | ||
readonly cachePromiseRejection?: boolean; | ||
} | ||
@@ -44,0 +58,0 @@ } |
34
index.js
'use strict'; | ||
const mimicFn = require('mimic-fn'); | ||
const isPromise = require('p-is-promise'); | ||
const mapAgeCleaner = require('map-age-cleaner'); | ||
@@ -8,23 +7,5 @@ | ||
const defaultCacheKey = (...arguments_) => { | ||
if (arguments_.length === 0) { | ||
return '__defaultKey'; | ||
} | ||
if (arguments_.length === 1) { | ||
const [firstArgument] = arguments_; | ||
const isObject = typeof firstArgument === 'object' && firstArgument !== null; | ||
const isPrimitive = !isObject; | ||
if (isPrimitive) { | ||
return firstArgument; | ||
} | ||
} | ||
return JSON.stringify(arguments_); | ||
}; | ||
const mem = (fn, { | ||
cacheKey = defaultCacheKey, | ||
cacheKey = ([firstArgument]) => firstArgument, | ||
cache = new Map(), | ||
cachePromiseRejection = true, | ||
maxAge | ||
@@ -37,3 +18,3 @@ } = {}) => { | ||
const memoized = function (...arguments_) { | ||
const key = cacheKey(...arguments_); | ||
const key = cacheKey(arguments_); | ||
@@ -51,6 +32,2 @@ if (cache.has(key)) { | ||
if (isPromise(cacheItem) && cachePromiseRejection === false) { | ||
cacheItem.catch(() => cache.delete(key)); | ||
} | ||
return cacheItem; | ||
@@ -73,7 +50,10 @@ }; | ||
module.exports.clear = fn => { | ||
if (!cacheStore.has(fn)) { | ||
throw new Error('Can\'t clear a function that was not memoized!'); | ||
} | ||
const cache = cacheStore.get(fn); | ||
if (cache && typeof cache.clear === 'function') { | ||
if (typeof cache.clear === 'function') { | ||
cache.clear(); | ||
} | ||
}; |
{ | ||
"name": "mem", | ||
"version": "5.1.1", | ||
"version": "6.0.0", | ||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", | ||
@@ -37,11 +37,11 @@ "license": "MIT", | ||
"map-age-cleaner": "^0.1.3", | ||
"mimic-fn": "^2.1.0", | ||
"p-is-promise": "^2.1.0" | ||
"mimic-fn": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"ava": "^2.4.0", | ||
"delay": "^4.1.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
"serialize-javascript": "^2.1.0", | ||
"tsd": "^0.10.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -5,5 +5,7 @@ # mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem) | ||
Memory is automatically released when an item expires. | ||
Memory is automatically released when an item expires or the cache is cleared. | ||
By default, **only the first argument is considered** and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). If you need to cache multiple arguments or cache `object`s *by value*, use the `cacheKey` option. | ||
## Install | ||
@@ -28,7 +30,7 @@ | ||
// Cached as it's the same arguments | ||
// Cached as it's the same argument | ||
memoized('foo'); | ||
//=> 1 | ||
// Not cached anymore as the arguments changed | ||
// Not cached anymore as the argument changed | ||
memoized('bar'); | ||
@@ -39,2 +41,6 @@ //=> 2 | ||
//=> 2 | ||
// Only the first argument is considered by default | ||
memoized('bar', 'foo'); | ||
//=> 2 | ||
``` | ||
@@ -98,3 +104,3 @@ | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: `Infinity` | ||
@@ -106,11 +112,30 @@ | ||
Type: `Function` | ||
Type: `Function`\ | ||
Default: `arguments_ => arguments_[0]`\ | ||
Example: `arguments_ => JSON.stringify(arguments_)` | ||
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 (if it's a `function`, its reference will be used as key), otherwise it's all the function arguments JSON stringified as an array. | ||
Determines the cache key for storing the result based on the function arguments. By default, **only the first argument is considered**. | ||
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`. | ||
A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option). | ||
You can have it cache **all** the arguments by value with `JSON.stringify`, if they are compatible: | ||
```js | ||
const mem = require('mem'); | ||
mem(function_, {cacheKey: JSON.stringify}); | ||
``` | ||
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for `RegExp`, `Date` and so on. | ||
```js | ||
const mem = require('mem'); | ||
const serializeJavascript = require('serialize-javascript'); | ||
mem(function_, {cacheKey: serializeJavascript}); | ||
``` | ||
##### cache | ||
Type: `object`<br> | ||
Type: `object`\ | ||
Default: `new Map()` | ||
@@ -120,9 +145,2 @@ | ||
##### cachePromiseRejection | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Cache rejected promises. | ||
### mem.clear(fn) | ||
@@ -129,0 +147,0 @@ |
10414
2
194
5
125
+ Addedmimic-fn@3.1.0(transitive)
- Removedp-is-promise@^2.1.0
- Removedmimic-fn@2.1.0(transitive)
- Removedp-is-promise@2.1.0(transitive)
Updatedmimic-fn@^3.0.0