memoize-one
Advanced tools
Comparing version 6.0.0-beta.1 to 6.0.0
export declare type EqualityFn<TFunc extends (...args: any[]) => any> = (newArgs: Parameters<TFunc>, lastArgs: Parameters<TFunc>) => boolean; | ||
declare type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
export declare type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
clear: () => void; | ||
@@ -4,0 +4,0 @@ (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; |
{ | ||
"name": "memoize-one", | ||
"version": "6.0.0-beta.1", | ||
"version": "6.0.0", | ||
"description": "A memoization library which only remembers the latest invocation", | ||
@@ -54,3 +54,2 @@ "main": "dist/memoize-one.cjs.js", | ||
"benchmark": "^2.1.4", | ||
"cli-table": "^0.3.6", | ||
"cross-env": "^7.0.3", | ||
@@ -66,2 +65,3 @@ "eslint": "7.32.0", | ||
"lodash.memoize": "^4.1.2", | ||
"markdown-table": "^3.0.1", | ||
"mem": "^9.0.1", | ||
@@ -68,0 +68,0 @@ "memoizee": "^0.4.15", |
180
README.md
@@ -5,8 +5,4 @@ # memoize-one | ||
> Also [async version](https://github.com/microlinkhq/async-memoize-one). | ||
[![Build Status](https://travis-ci.org/alexreardon/memoize-one.svg?branch=master)](https://travis-ci.org/alexreardon/memoize-one) | ||
[![npm](https://img.shields.io/npm/v/memoize-one.svg)](https://www.npmjs.com/package/memoize-one) | ||
![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet) | ||
[![dependencies](https://david-dm.org/alexreardon/memoize-one.svg)](https://david-dm.org/alexreardon/memoize-one) | ||
[![minzip](https://img.shields.io/bundlephobia/minzip/memoize-one.svg)](https://www.npmjs.com/package/memoize-one) | ||
@@ -19,2 +15,4 @@ [![Downloads per month](https://img.shields.io/npm/dm/memoize-one.svg)](https://www.npmjs.com/package/memoize-one) | ||
> For working with promises, [@Kikobeats](https://github.com/Kikobeats) has built [async-memoize-one](https://github.com/microlinkhq/async-memoize-one). | ||
## Usage | ||
@@ -133,3 +131,3 @@ | ||
Here is an example that uses a [dequal](https://github.com/lukeed/dequal) deep equal equality check | ||
Here is an example that uses a [lodash.isEqual](https://lodash.com/docs/4.17.15#isEqual) deep equal equality check | ||
@@ -356,4 +354,4 @@ > `lodash.isequal` correctly handles deep comparing two arrays | ||
- Clear memory | ||
- Cause the underlying function to be called again without having to change arguments | ||
- Release memory | ||
- Allow the underlying function to be called again without having to change arguments | ||
@@ -475,2 +473,60 @@ ```ts | ||
## Memoized function `type` | ||
The resulting function you get back from `memoize-one` has *almost* the same `type` as the function that you are memoizing | ||
```ts | ||
declare type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
clear: () => void; | ||
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; | ||
}; | ||
``` | ||
- the same call signature as the function being memoized | ||
- a `.clear()` function property added | ||
- other function object properties on `TFunc` as not carried over | ||
You are welcome to use the `MemoizedFn` generic directly from `memoize-one` if you like: | ||
```ts | ||
import memoize, { MemoizedFn } from 'memoize-one'; | ||
import isDeepEqual from 'lodash.isequal'; | ||
import { expectTypeOf } from 'expect-type'; | ||
// Takes any function: TFunc, and returns a Memoized<TFunc> | ||
function withDeepEqual<TFunc extends (...args: any[]) => any>(fn: TFunc): MemoizedFn<TFunc> { | ||
return memoize(fn, isDeepEqual); | ||
} | ||
function add(first: number, second: number): number { | ||
return first + second; | ||
} | ||
const memoized = withDeepEqual(add); | ||
expectTypeOf<typeof memoized>().toEqualTypeOf<MemoizedFn<typeof add>>(); | ||
``` | ||
In this specific example, this type would have been correctly inferred too | ||
```ts | ||
import memoize, { MemoizedFn } from 'memoize-one'; | ||
import isDeepEqual from 'lodash.isequal'; | ||
import { expectTypeOf } from 'expect-type'; | ||
// return type of MemoizedFn<TFunc> is inferred | ||
function withDeepEqual<TFunc extends (...args: any[]) => any>(fn: TFunc) { | ||
return memoize(fn, isDeepEqual); | ||
} | ||
function add(first: number, second: number): number { | ||
return first + second; | ||
} | ||
const memoized = withDeepEqual(add); | ||
// type test still passes | ||
expectTypeOf<typeof memoized>().toEqualTypeOf<MemoizedFn<typeof add>>(); | ||
``` | ||
## Performance 🚀 | ||
@@ -492,3 +548,3 @@ | ||
node version `14.15.0` | ||
node version `16.11.1` | ||
@@ -502,71 +558,71 @@ You can run this test in the repo by: | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | memoize-one | 80,657,220 | | ||
| 2 | moize | 64,951,103 | | ||
| 3 | memoizee | 32,066,963 | | ||
| 4 | lodash.memoize | 30,387,390 | | ||
| 5 | mem (JSON.stringify strategy) | 3,894,072 | | ||
| 6 | no memoization | 506 | | ||
| 7 | fast-memoize | 505 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | memoize-one | 80,112,981 | | ||
| 2 | moize | 72,885,631 | | ||
| 3 | memoizee | 35,550,009 | | ||
| 4 | mem (JSON.stringify strategy) | 4,610,532 | | ||
| 5 | lodash.memoize (JSON.stringify key resolver) | 3,708,945 | | ||
| 6 | no memoization | 505 | | ||
| 7 | fast-memoize | 504 | | ||
**single primitive argument** | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | fast-memoize | 43,922,254 | | ||
| 2 | lodash.memoize | 26,652,387 | | ||
| 3 | moize | 25,654,686 | | ||
| 4 | memoize-one | 25,059,187 | | ||
| 5 | memoizee | 19,096,104 | | ||
| 6 | mem (JSON.stringify strategy) | 3,448,488 | | ||
| 7 | no memoization | 503 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | fast-memoize | 45,482,711 | | ||
| 2 | moize | 34,810,659 | | ||
| 3 | memoize-one | 29,030,828 | | ||
| 4 | memoizee | 23,467,065 | | ||
| 5 | mem (JSON.stringify strategy) | 3,985,223 | | ||
| 6 | lodash.memoize (JSON.stringify key resolver) | 3,369,297 | | ||
| 7 | no memoization | 507 | | ||
**single complex argument** | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | moize | 31,199,164 | | ||
| 2 | lodash.memoize | 28,712,860 | | ||
| 3 | memoize-one | 23,896,851 | | ||
| 4 | memoizee | 19,010,167 | | ||
| 5 | mem (JSON.stringify strategy) | 2,045,973 | | ||
| 6 | fast-memoize | 1,519,294 | | ||
| 7 | no memoization | 504 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | moize | 27,660,856 | | ||
| 2 | memoize-one | 22,407,916 | | ||
| 3 | memoizee | 19,546,835 | | ||
| 4 | mem (JSON.stringify strategy) | 2,068,038 | | ||
| 5 | lodash.memoize (JSON.stringify key resolver) | 1,911,335 | | ||
| 6 | fast-memoize | 1,633,855 | | ||
| 7 | no memoization | 504 | | ||
**multiple primitive arguments** | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | moize | 21,039,928 | | ||
| 2 | lodash.memoize | 20,248,759 | | ||
| 3 | memoize-one | 16,600,643 | | ||
| 4 | memoizee | 9,071,600 | | ||
| 5 | mem (JSON.stringify strategy) | 2,990,592 | | ||
| 6 | fast-memoize | 1,156,061 | | ||
| 7 | no memoization | 506 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | moize | 22,366,497 | | ||
| 2 | memoize-one | 17,241,995 | | ||
| 3 | memoizee | 9,789,442 | | ||
| 4 | mem (JSON.stringify strategy) | 3,065,328 | | ||
| 5 | lodash.memoize (JSON.stringify key resolver) | 2,663,599 | | ||
| 6 | fast-memoize | 1,219,548 | | ||
| 7 | no memoization | 504 | | ||
**multiple complex arguments** | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | lodash.memoize | 22,803,155 | | ||
| 2 | moize | 19,773,333 | | ||
| 3 | memoize-one | 16,341,253 | | ||
| 4 | memoizee | 9,030,317 | | ||
| 5 | mem (JSON.stringify strategy) | 806,040 | | ||
| 6 | fast-memoize | 633,057 | | ||
| 7 | no memoization | 504 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | moize | 21,788,081 | | ||
| 2 | memoize-one | 17,321,248 | | ||
| 3 | memoizee | 9,595,420 | | ||
| 4 | lodash.memoize (JSON.stringify key resolver) | 873,283 | | ||
| 5 | mem (JSON.stringify strategy) | 850,779 | | ||
| 6 | fast-memoize | 687,863 | | ||
| 7 | no memoization | 504 | | ||
**multiple complex arguments (spreading arguments)** | ||
| Position | Library | Operations per second | | ||
| -------- | ----------------------------- | --------------------- | | ||
| 1 | lodash.memoize | 24,089,032 | | ||
| 2 | moize | 21,574,025 | | ||
| 3 | memoizee | 19,810,230 | | ||
| 4 | memoize-one | 16,201,443 | | ||
| 5 | mem (JSON.stringify strategy) | 861,279 | | ||
| 6 | fast-memoize | 656,715 | | ||
| 7 | no memoization | 504 | | ||
| Position | Library | Operations per second | | ||
| -------- | -------------------------------------------- | --------------------- | | ||
| 1 | moize | 21,701,537 | | ||
| 2 | memoizee | 19,463,942 | | ||
| 3 | memoize-one | 17,027,544 | | ||
| 4 | lodash.memoize (JSON.stringify key resolver) | 887,816 | | ||
| 5 | mem (JSON.stringify strategy) | 849,244 | | ||
| 6 | fast-memoize | 691,512 | | ||
| 7 | no memoization | 504 | | ||
@@ -573,0 +629,0 @@ </p> |
@@ -8,2 +8,8 @@ import areInputsEqual from './are-inputs-equal'; | ||
export type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
clear: () => void; | ||
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; | ||
}; | ||
// internal type | ||
type Cache<TFunc extends (this: any, ...args: any[]) => any> = { | ||
@@ -15,7 +21,2 @@ lastThis: ThisParameterType<TFunc>; | ||
type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
clear: () => void; | ||
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; | ||
}; | ||
function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>( | ||
@@ -22,0 +23,0 @@ resultFn: TFunc, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
35613
247
0
631