1.0.0 • Published 10 months ago

tiny-mem v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

tiny-mem

Minimal, no-dependencies memoization helper.

Motivation

Lodash has a handy memoize function but it also has a ton of other functions. Sindre Sorhus' mem is great but is also full of features you may not need.

This package is a simple, memoizing function implementation that does just that. No fancy options, just like an MVP.

Install

npm install tiny-mem

Usage

const memoize = require('tiny-mem')

let index = 0
const seq = () => ++index
const memoized = memoize(seq)

memoized('one') // Returns 1

memoized('two') // Returns 2
memoized('two') // Returns 2 again

memoized('one') // Returns 1 again

memoized('two', 'one') // Still returns 2

API

memoize(fn, options?)

Returns a memoized function.

fn

Type: Function

A function.

options?

Type: object

options.cache?

Type: object Default: new Map()

The cache storage. Must implement these methods: has(key), set(key, value), get(key) and delete(key)

options.resolver?

Type: Function Default: (...args) => args[0]

Determines how the caching key will be computed. By default, it will only consider the first argument and use strict equality to evaluate a match.

memoizedFn

The memoized version of fn. It resolves to the result of calling fn(), which is called only once per key.

1.0.0

10 months ago