> moize
<img src="https://img.shields.io/badge/build-passing-brightgreen.svg"/>
<img src="https://img.shields.io/badge/coverage-100%25-brightgreen.svg"/>
<img src="https://img.shields.io/badge/license-MIT-blue.svg"/>
`moize` is a [consistently blazing fast](#benchmarks) memoization library for JavaScript. It handles multiple parameters (including default values) without any additional configuration, and offers a large number of options to satisfy any number of potential use-cases.
- [Importing](#importing)
- [ESM in browsers](#esm-in-browsers)
- [ESM in NodeJS](#esm-in-nodejs)
- [CommonJS](#commonjs)
- [Usage](#usage)
- [Configuration options](#configuration-options)
- [isDeepEqual](#isdeepequal)
- [isPromise](#ispromise)
- [isReact](#isreact)
- [isSerialized](#isserialized)
- [isShallowEqual](#isshallowequal)
- [matchesArg](#matchesarg)
- [matchesKey](#matcheskey)
- [maxAge](#maxage)
- [maxArgs](#maxargs)
- [maxSize](#maxsize)
- [onCacheAdd](#oncacheadd)
- [onCacheChange](#oncachechange)
- [onCacheHit](#oncachehit)
- [onExpire](#onexpire)
- [profileName](#profilename)
- [serializer](#serializer)
- [transformArgs](#transformargs)
- [updateCacheForKey](#updatecacheforkey)
- [updateExpire](#updateexpire)
- [Usage with shortcut methods](#usage-with-shortcut-methods)
- [moize.deep](#moizedeep)
- [moize.infinite](#moizeinfinite)
- [moize.matchesArg](#moizematchesarg)
- [moize.matchesKey](#moizematcheskey)
- [moize.maxAge](#moizemaxage)
- [moize.maxArgs](#moizemaxargs)
- [moize.maxSize](#moizemaxsize)
- [moize.profile](#moizeprofile)
- [moize.promise](#moizepromise)
- [moize.react](#moizereact)
- [moize.serialize](#moizeserialize)
- [moize.serializeWith](#moizeserializewith)
- [moize.shallow](#moizeshallow)
- [moize.transformArgs](#moizetransformargs)
- [moize.updateCacheForKey](#moizeupdatecacheforkey)
- [useMoize hook](#usemoize-hook)
- [Composition](#composition)
- [Collecting statistics](#collecting-statistics)
- [Stats methods](#stats-methods)
- [clearStats](#clearstats)
- [collectStats](#collectstats)
- [getStats([profileName])](#getstatsprofilename)
- [Introspection](#introspection)
- [isCollectingStats](#iscollectingstats)
- [isMoized](#ismoized)
- [Direct cache manipulation](#direct-cache-manipulation)
- [cache](#cache)
- [cacheSnapshot](#cachesnapshot)
- [add(key, value)](#addkey-value)
- [clear()](#clear)
- [get(key)](#getkey)
- [getStats()](#getstats)
- [has(key)](#haskey)
- [keys()](#keys)
- [remove(key)](#removekey)
- [update(key, value)](#updatekey-value)
- [values()](#values)
- [Benchmarks](#benchmarks)
- [Filesize](#filesize)
- [Browser support](#browser-support)
- [Development](#development)
```
$ npm i moize --save
```
# Importing
## ESM in browsers
```ts
import moize from 'moize';
```
## ESM in NodeJS
```ts
import moize from 'moize/mjs/index.mjs';
```
## CommonJS
```ts
const moize = require('moize');
```
# Usage
```ts
import moize from 'moize';
const method = (a: number, b: number) => a + b;
const memoized = moize(method);
memoized(2, 4); // 6
memoized(2, 4); // 6, pulled from cache
```
All parameter types are supported, including circular objects, functions, etc. There are also a number of [shortcut methods](#usage-with-shortcut-methods) to memoize for unique use-cases.
# Configuration options
`moize` optionally accepts an object of options as either the second parameter or as the first step in a curried function:
```ts
// inline
moize(fn, options);
// curried
moize(options)(fn);
```
The full shape of these options:
```ts
type Options = {
// is the cache based on deep equality of each key argument
isDeepEqual: boolean;
// is the result a promise
isPromise: boolean;
// is the result a React component
isReact: boolean;
// should the parameters be serialized instead of directly referenced
isSerialized: boolean;
// is the cache based on shallow equality of each key argument
isShallowEqual: boolean;
// custom method to compare equality between two key arguments
matchesArg: (cachedKeyArg: any, keyArg: any) => boolean;
// custom method to compare equality across all key arguments
matchesKey: (cachedKey: any[], key: any[]) => boolean;
// amount of time in milliseconds before the cache will expire
maxAge: number;
// maximum number of arguments passed to use as key for caching
maxArgs: number;
// maximum size of cache for this method
maxSize: number;
// method fired when a new entry is added to cache
onCacheAdd: (
cache: moize.Cache,
options: moize.Options,
moized: (...args: any[]) => any
) => void;
// method fire when either a new entry is added to cache or the LRU ordering of the cache has changed
onCacheChange: (
cache: moize.Cache,
options: moize.Options,
moized: (...args: any[]) => any
) => void;
// method fired when an existing entry in cache is used
onCacheHit: (
cache: moize.Cache,
options: moize.Options,
moized: (...args: any[]) => any
) => void;
// method to fire when a cache entry expires (in combination with maxAge)
onExpire: (key: any[]) => void;
// the unique identifier to give the memoized method when collecting statistics
profileName: string;
// method to serialize the arguments to build a unique cache key
serializer: (key: any[]) => string;
// method to transform the args into a custom format for key storage in cache
transformArgs: (key: any[]) => any[];
// should the cache entry be refreshed by calling the underlying function with the same parameters and
// updating the value stored in cache to be the new result
updateCacheForKey: (key: any[]) => boolean;
// should the cache entry's expiration be refreshed when the cache entry is hit (in combination with maxAge)
updateExpire: boolean;
};
```
All default values can be found [here](src/constants.ts).
## isDeepEqual
_defaults to false_
Should deep equality be used to compare cache each key argument.
```ts
type Arg = {
one: {
nested: string;
};
two: string;
};
const fn = ({ one, two }: Arg) => [one, two];
const memoized = moize(fn, { isDeepEqual: true });
memoized({ one: { nested: 'one' }, two: 'two' });
memoized({ one: { nested: 'one' }, two: 'two' }); // pulls from cache
```
This is also available via the shortcut method of [`moize.deep`](#moizedeep)
```ts
const memoized = moize.deep(fn);
```
## isPromise
_defaults to false_
Is the computed value in the function a `Promise`.
```ts
const fn = async (item: Promise<string>) => await item;
const memoized = moize(fn, { isPromise: true });
```
This is also available via the shortcut method of [`moize.promise`](#moizepromise).
```ts
const memoized = -moize.promise(fn);
```
The `Promise` itself will be stored in cache, so that cached returns will always maintain the `Promise` contract. For common usage reasons, if the `Promise` is rejected, the cache entry will be deleted.
## isReact
_defaults to false_
Is the function passed a stateless functional `React` component.
```tsx
type Props = {
one: string;
two: number;
};
const Component = ({ one, two }: Props) => (
<div>
{one}: {two}
</div>
);
const MemoizedFoo = moize(Component, { isReact: true });
```
This is also available via the shortcut method of [`moize.react`](#moizereact).
```ts
const MemoizedFoo = moize.react(Component);
```
The method will do a shallow equal comparison of both `props` and legacy `context` of the component based on strict equality. If you want to do a deep equals comparison, set [`isDeepEqual`](#isdeepequal) to true.
**NOTE**: This will memoize on each instance of the component passed, which is equivalent to `PureComponent` or `React.memo`. If you want to
memoize on _all_ instances (which is how this option worked prior to versio
没有合适的资源?快使用搜索试试~ 我知道了~
博客系列:Hexo+Github博客搭建教程
共2000个文件
js:1495个
md:269个
css:145个
0 下载量 168 浏览量
2024-01-03
16:40:01
上传
评论
收藏 16.97MB ZIP 举报
温馨提示
博客系列:Hexo+Github博客搭建教程
资源推荐
资源详情
资源评论
收起资源包目录
博客系列:Hexo+Github博客搭建教程 (2000个子文件)
prism-treeview.css 8KB
prism-treeview.min.css 7KB
prism-previewers.css 5KB
prism-previewers.min.css 4KB
prism-coy.css 4KB
nord.css 4KB
prism-twilight.css 4KB
night-owl.css 3KB
prism-coy.min.css 3KB
nord.min.css 3KB
prism-solarizedlight.css 3KB
prism-funky.css 2KB
prism-twilight.min.css 2KB
prism.css 2KB
tokyo-night-dark.css 2KB
purebasic.css 2KB
github-dark.css 2KB
github.css 2KB
stackoverflow-light.css 2KB
stackoverflow-dark.css 2KB
github-dark-dimmed.css 2KB
prism-dark.css 2KB
prism-funky.min.css 2KB
default.css 2KB
prism-okaidia.css 2KB
prism.min.css 2KB
prism-tomorrow.css 2KB
grayscale.min.css 2KB
nnfx-dark.css 2KB
prism-solarizedlight.min.css 2KB
prism-toolbar.css 2KB
prism-line-highlight.css 2KB
prism-dark.min.css 1KB
intellij-light.css 1KB
a11y-dark.css 1KB
vs2015.css 1KB
nnfx-light.min.css 1KB
panda-syntax-light.css 1KB
isbl-editor-dark.css 1KB
prism-okaidia.min.css 1KB
isbl-editor-light.css 1KB
srcery.css 1KB
agate.min.css 1KB
felipec.css 1KB
gradient-light.css 1KB
atom-one-light.css 1KB
github-dark.min.css 1KB
atom-one-dark.css 1KB
gradient-dark.css 1KB
prism-tomorrow.min.css 1KB
github.min.css 1KB
devibeans.css 1KB
hybrid.css 1KB
stackoverflow-light.min.css 1KB
stackoverflow-dark.min.css 1KB
github-dark-dimmed.min.css 1KB
tokyo-night-dark.min.css 1KB
an-old-hope.css 1KB
sunburst.css 1KB
prism-toolbar.min.css 1KB
idea.css 1KB
pojoaque.css 1KB
tomorrow-night-blue.css 1KB
a11y-dark.min.css 1KB
a11y-light.min.css 1KB
prism-line-highlight.min.css 1KB
docco.css 1KB
foundation.css 1KB
obsidian.css 1KB
vs2015.min.css 1KB
kimbie-light.css 1KB
kimbie-dark.css 1KB
panda-syntax-light.min.css 1KB
monokai-sublime.css 1KB
prism-inline-color.css 1KB
lioshi.css 1KB
xt256.css 1KB
paraiso-light.css 1024B
paraiso-dark.css 1023B
rainbow.css 1022B
qtcreator-dark.css 1012B
qtcreator-light.css 1011B
gml.css 973B
isbl-editor-dark.min.css 971B
monokai.css 971B
an-old-hope.min.css 961B
isbl-editor-light.min.css 952B
sunburst.min.css 950B
prism-command-line.css 949B
magula.css 929B
color-brewer.css 924B
idea.min.css 906B
hybrid.min.css 897B
ir-black.css 895B
brown-paper.css 891B
far.css 871B
routeros.min.css 862B
atom-one-dark.min.css 856B
shades-of-purple.min.css 854B
dark.css 846B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
坦笑&&life
- 粉丝: 2w+
- 资源: 1405
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功