Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stagas/memoize-pure

low footprint memoize for just pure sync functions with scalar arguments
https://github.com/stagas/memoize-pure

memoize

Last synced: 13 days ago
JSON representation

low footprint memoize for just pure sync functions with scalar arguments

Awesome Lists containing this project

README

        

memoize-pure


low footprint memoize for just pure sync functions with scalar arguments


🔧 Install
· 🧩 Example
· 📜 API docs
· 🔥 Releases
· 💪🏼 Contribute
· 🖐️ Help

***

## Install

```sh
$ npm i memoize-pure
```

## API

#### Table of Contents

* [memoize](#memoize)
* [Parameters](#parameters)
* [memoizeDebug](#memoizedebug)
* [Parameters](#parameters-1)

### memoize

[src/index.ts:16-26](https://github.com/stagas/memoize-pure/blob/b06373fb2b76ba49c486d0189e8c4a31d728276d/src/index.ts#L16-L26 "Source code on GitHub")

Memoize a function.

```ts
const fn = memoize((a, b, c) => some_expensive_calls(a, b, c))
...
const result = fn(1, 2, 3) // => calls the inner function and saves arguments signature "1,2,3"
...
const result = fn(1, 2, 3) // => returns the memoized result immediately since "1,2,3" matches memory
```

#### Parameters

* `fn` **any** The function to memoize
* `map` A map object to use as memory (optional, default `Object.create(null)`)

Returns **any** The memoized function

### memoizeDebug

[src/index.ts:48-89](https://github.com/stagas/memoize-pure/blob/b06373fb2b76ba49c486d0189e8c4a31d728276d/src/index.ts#L48-L89 "Source code on GitHub")

Debug memoize a function.

```ts
const fn = memoizeDebug((a, b, c) => some_expensive_calls(a, b, c))
...
const result = fn(1, 2, 3) // => calls the inner function and saves arguments signature "1,2,3"
...
const result = fn(1, 2, 3) // => returns the memoized result immediately since "1,2,3" matches memory
fn.__memoizeTimesCalled__ // => 1
fn.__memoizeMap__ // => { '1,2,3': 'some result' }
```

#### Parameters

* `fn` **any** The function to memoize
* `map` (optional, default `Object.create(null)`)
* `threshold` (optional, default `Infinity`)

Returns **any** The memoized function including two properties:* `__memoizeMap__` is the memory map of arguments and results
* `__memoizeTimesCalled__` is the count that the wrapped function has been called

## Contribute

[Fork](https://github.com/stagas/memoize-pure/fork) or
[edit](https://github.dev/stagas/memoize-pure) and submit a PR.

All contributions are welcome!

## License

MIT © 2021
[stagas](https://github.com/stagas)