mem
- Version 10.0.0
- Published
- 17.9 kB
- 2 dependencies
- MIT license
Install
npm i mem
yarn add mem
pnpm add mem
Overview
Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
Index
Functions
Interfaces
Functions
function mem
mem: <FunctionToMemoize extends AnyFunction, CacheKeyType>( fn: FunctionToMemoize, { cacheKey, cache, maxAge }?: Options<FunctionToMemoize, CacheKeyType>) => FunctionToMemoize;
[Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
Parameter fn
Function to be memoized.
Example 1
import mem from 'mem';let index = 0;const counter = () => ++index;const memoized = mem(counter);memoized('foo');//=> 1// Cached as it's the same argumentmemoized('foo');//=> 1// Not cached anymore as the arguments changedmemoized('bar');//=> 2memoized('bar');//=> 2
function memClear
memClear: (fn: AnyFunction) => void;
Clear all cached data of a memoized function.
Parameter fn
Memoized function.
function memDecorator
memDecorator: <FunctionToMemoize extends AnyFunction, CacheKeyType>( options?: Options<FunctionToMemoize, CacheKeyType>) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
Returns
A [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
Example 1
import {memDecorator} from 'mem';class Example {index = 0@memDecorator()counter() {return ++this.index;}}class ExampleWithOptions {index = 0@memDecorator({maxAge: 1000})counter() {return ++this.index;}}
Interfaces
interface Options
interface Options<FunctionToMemoize extends AnyFunction, CacheKeyType> {}
property cache
readonly cache?: CacheStorage<CacheKeyType, ReturnType<FunctionToMemoize>>;
Use a different cache storage. Must implement the following methods:
.has(key)
,.get(key)
,.set(key, value)
,.delete(key)
, and optionally.clear()
. You could for example use aWeakMap
instead or [quick-lru
](https://github.com/sindresorhus/quick-lru) for a LRU cache.new Map()
Example 1
new WeakMap()
property cacheKey
readonly cacheKey?: (arguments_: Parameters<FunctionToMemoize>) => CacheKeyType;
Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
A
cacheKey
function can return any type supported byMap
(or whatever structure you use in thecache
option).You can have it cache **all** the arguments by value with
JSON.stringify
, if they are compatible:``` import mem from 'mem';
mem(function_, {cacheKey: JSON.stringify}); ```
Or you can use a more full-featured serializer like [serialize-javascript](https://github.com/yahoo/serialize-javascript) to add support for
RegExp
,Date
and so on.``` import mem from 'mem'; import serializeJavascript from 'serialize-javascript';
mem(function_, {cacheKey: serializeJavascript}); ```
arguments_ => arguments_[0]
Example 1
arguments_ => JSON.stringify(arguments_)
property maxAge
readonly maxAge?: number;
Milliseconds until the cache expires.
Infinity
Package Files (1)
Dependencies (2)
Dev Dependencies (11)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/mem
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/mem)
- HTML<a href="https://www.jsdocs.io/package/mem"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2698 ms. - Missing or incorrect documentation? Open an issue for this package.