# trie-memoize

> A memoization algorithm in which each function argument represents a new key, creating a trie of caches as defined by the array in your first argument.

Latest version **1.2.0** (published 2020-06-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install trie-memoize
pnpm add trie-memoize
yarn add trie-memoize
bun add trie-memoize
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2020-06-04 |
| First published | 2019-02-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | trie, memo, memoize, memoizer, memoization, cache, trie memoize |

## Links

- npm: https://www.npmjs.com/package/trie-memoize
- Repository: https://github.com/jaredLunde/trie-memoize
- Homepage: https://github.com/jaredLunde/trie-memoize#readme
- Issues: https://github.com/jaredLunde/trie-memoize/issues
- npm.io page: https://npm.io/package/trie-memoize

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2020-06-04
- 1.1.5 — 2020-05-03
- 1.1.4 — 2020-04-26
- 1.1.3 — 2020-04-26
- 1.1.2 — 2020-04-10
- 1.1.1 — 2020-01-25
- 1.1.0 — 2020-01-18
- 1.0.19 — 2019-11-26
- 1.0.18 — 2019-11-26
- 1.0.17 — 2019-11-26
- 1.0.16 — 2019-11-26
- 1.0.15 — 2019-08-12
- 1.0.14 — 2019-08-11
- 1.0.13 — 2019-08-10
- 1.0.12 — 2019-08-10
- … 12 more at https://npm.io/package/trie-memoize/versions

## README

<hr>
<div align="center">
  <h1 align="center">
    trie-memoize
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=trie-memoize">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/trie-memoize">
    <img alt="Types" src="https://img.shields.io/npm/types/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Code coverage report" href="https://codecov.io/gh/jaredLunde/trie-memoize">
    <img alt="Code coverage" src="https://img.shields.io/codecov/c/gh/jaredLunde/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.org/jaredLunde/trie-memoize">
    <img alt="Build status" src="https://img.shields.io/travis/jaredLunde/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="NPM version" href="https://www.npmjs.com/package/trie-memoize">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/trie-memoize?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i trie-memoize</pre>
<hr>

A memoization algorithm in which each function argument represents a new
key in a mapping, creating a trie of caches - the depth of which defined
by your setup. It is very quick with its `O(n arguments)` lookup
performance and is memory-efficient, particularly when `WeakMaps` are used.

This memoization function only works for functions where the exact
number of arguments is known and constant.

## Quick Start

```js
const m1 = memoize([{}], (v) => v.toUpperCase())
m1('foo') // FOO uncached
m1('foo') // FOO cached

const m2 = memoize([{}, Map], (v1, v2) => `${v1}-${v2}`)
m2('foo', 'bar') // foo-bar, uncached
m2('foo', 'bar') // foo-bar, cached

const m3 = memoize(
  [WeakMap, Map, WeakMap],
  (v1, v2, v3) =>
    `${JSON.stringify(v1)}-${JSON.stringify(v1)}-${JSON.stringify(v3)}`
)

const v1 = {}
const v2 = 'foo'
const v3 = {}
m3(v1, v2, v3) // {}-"foo"-{} uncached
m3(v1, v2, v3) // {}-"foo"-{} cached
```

## API

### `memoize(caches, fn)`

| Argument | Type                 | Description                                                                                                                                                                                                                                                                                                                                                                                                     |
| -------- | -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| caches   | `CacheConstructor[]` | An array of plain objects or map-like constructors (Map, WeakMap, some custom map w/ get + set methods) used for caching each level of the tree. The first array element will be the cache for the first argument of the function, call, and so on. Therefore, the length of this array must be the same as the length of arguments your memoized function accepts, or at least as deep as you'd like to cache. |
| fn       | `(...args: T) => U`  | The function you'd like to memoize                                                                                                                                                                                                                                                                                                                                                                              |

### Types

#### CacheConstructor

```ts
export type CacheConstructor =
  | MapConstructor
  | WeakMapConstructor
  | MapLike
  | Record<any, any>
```

## LICENSE

MIT

---
_Source: https://npm.io/package/trie-memoize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
