Skip to content
generated from stagas/ts

low footprint memoize for just pure sync functions with scalar arguments

Notifications You must be signed in to change notification settings

stagas/memoize-pure

Repository files navigation

memoize-pure

low footprint memoize for just pure sync functions with scalar arguments

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


Install

$ npm i memoize-pure

API

Table of Contents

memoize

src/index.ts:16-26

Memoize a function.

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

Debug memoize a function.

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 or edit and submit a PR.

All contributions are welcome!

License

MIT © 2021 stagas

About

low footprint memoize for just pure sync functions with scalar arguments

Topics

Resources

Stars

Watchers

Forks

Packages