a-synchronous / a-synchronous/rubico

rubico/x/memoize

未关闭
#105 0 条评论 0 个 reaction 已指派 1 人 已被 @richytong 认领 在 GitHub 查看
enhancement
主要语言
JavaScript
星标
283
派生
17
PR 合并指标
30 天内没有已合并 PR

描述

### memoize
```coffeescript
var key any,
cache Mapany>,
funcArgs ...any,
func ...funcArgs=>Promise|any,
maxCacheSize number,
cacheClearer (cache, ...funcArgs)=>Promise<>|key

memoize(func) -> memoized function
memoized.cache -> cache

memoize(func, options {
cacheSize: number,
}) -> memoizedCapped function

memoizedCapped.cache -> cache
```

Memoize (create a cached version of) a function using its first argument as the cache key. If `cacheSize` is provided, it is used to indicate the maximum size of the internal cache. If the number of items in the cache exceeds this number, the cache is cleared. If instead `cacheClearer` is provided, it is called with a reference to the internal cache and any arguments supplied to the memoized function.

Note: Any effects of `func` will not be rerun on cache hit.

```javascript
const identity = value => value

const square = number => number ** 2

const memoizedCapped500Square = memoize(square, { cacheSize: 500 })

console.log(memoizedCapped500Square(3)) // 9
console.log(memoizedCapped500Square.cache) // Map { 3 => 9 }
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。