micro-memoize
Advanced tools
Comparing version 4.0.14 to 4.1.2
# micro-memoize CHANGELOG | ||
## 4.1.2 | ||
- Republish of [#102](https://github.com/planttheidea/micro-memoize/pull/102) | ||
## 4.1.1 (Bad version - do not use) | ||
- [#102](https://github.com/planttheidea/micro-memoize/pull/102) - avoid publishing development-only files for less `node_modules` bloat | ||
## 4.1.0 | ||
#### Enhancements | ||
- Types now have direct exports instead of requiring the `MicroMemoize` namespace. That namespace has been labeled as deprecated, and will be removed in the next major version change in favor of the direct type exports. | ||
#### Bugfixes | ||
- [#97](https://github.com/planttheidea/micro-memoize/issues/97) - `src` files included in publish, and referenced from `*.d.ts` files | ||
- `mjs/*d.ts` files renamed to `mjs/*.d.mts` to align with NodeJS standard | ||
- [#101](https://github.com/planttheidea/micro-memoize/pull/101) - fixed benchmark using `mem` incorrectly for complex object parameters or multiple parameter calls | ||
## 4.0.15 | ||
- [#99](https://github.com/planttheidea/micro-memoize/issues/99) - `mjs` import does not have typings surfaced | ||
## 4.0.14 | ||
@@ -4,0 +28,0 @@ |
@@ -78,3 +78,3 @@ 'use strict'; | ||
function isMemoized(fn) { | ||
return (typeof fn === 'function' && fn.isMemoized); | ||
return typeof fn === 'function' && fn.isMemoized; | ||
} | ||
@@ -141,2 +141,5 @@ /** | ||
Object.defineProperty(Cache.prototype, "size", { | ||
/** | ||
* The number of cached [key,value] results. | ||
*/ | ||
get: function () { | ||
@@ -149,2 +152,7 @@ return this.keys.length; | ||
Object.defineProperty(Cache.prototype, "snapshot", { | ||
/** | ||
* A copy of the cache at a moment in time. This is useful | ||
* to compare changes over time, since the cache mutates | ||
* internally for performance reasons. | ||
*/ | ||
get: function () { | ||
@@ -161,9 +169,3 @@ return { | ||
/** | ||
* @function _getKeyIndexFromMatchingKey | ||
* | ||
* @description | ||
* gets the matching key index when a custom key matcher is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a custom key matcher is used. | ||
*/ | ||
@@ -190,9 +192,3 @@ Cache.prototype._getKeyIndexFromMatchingKey = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForMany | ||
* | ||
* @description | ||
* gets the matching key index when multiple keys are used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when multiple keys are used. | ||
*/ | ||
@@ -240,9 +236,3 @@ Cache.prototype._getKeyIndexForMany = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForSingle | ||
* | ||
* @description | ||
* gets the matching key index when a single key is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a single key is used. | ||
*/ | ||
@@ -271,10 +261,3 @@ Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
/** | ||
* @function orderByLru | ||
* | ||
* @description | ||
* order the array based on a Least-Recently-Used basis | ||
* | ||
* @param key the new key to move to the front | ||
* @param value the new value to move to the front | ||
* @param startingIndex the index of the item to move to the front | ||
* Order the array based on a Least-Recently-Used basis. | ||
*/ | ||
@@ -303,9 +286,4 @@ Cache.prototype.orderByLru = function (key, value, startingIndex) { | ||
/** | ||
* @function updateAsyncCache | ||
* | ||
* @description | ||
* update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed | ||
* | ||
* @param memoized the memoized function | ||
* Update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed. | ||
*/ | ||
@@ -337,3 +315,2 @@ Cache.prototype.updateAsyncCache = function (memoized) { | ||
// cache | ||
function createMemoizedFunction(fn, options) { | ||
@@ -340,0 +317,0 @@ if (options === void 0) { options = {}; } |
@@ -76,3 +76,3 @@ /** | ||
function isMemoized(fn) { | ||
return (typeof fn === 'function' && fn.isMemoized); | ||
return typeof fn === 'function' && fn.isMemoized; | ||
} | ||
@@ -139,2 +139,5 @@ /** | ||
Object.defineProperty(Cache.prototype, "size", { | ||
/** | ||
* The number of cached [key,value] results. | ||
*/ | ||
get: function () { | ||
@@ -147,2 +150,7 @@ return this.keys.length; | ||
Object.defineProperty(Cache.prototype, "snapshot", { | ||
/** | ||
* A copy of the cache at a moment in time. This is useful | ||
* to compare changes over time, since the cache mutates | ||
* internally for performance reasons. | ||
*/ | ||
get: function () { | ||
@@ -159,9 +167,3 @@ return { | ||
/** | ||
* @function _getKeyIndexFromMatchingKey | ||
* | ||
* @description | ||
* gets the matching key index when a custom key matcher is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a custom key matcher is used. | ||
*/ | ||
@@ -188,9 +190,3 @@ Cache.prototype._getKeyIndexFromMatchingKey = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForMany | ||
* | ||
* @description | ||
* gets the matching key index when multiple keys are used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when multiple keys are used. | ||
*/ | ||
@@ -238,9 +234,3 @@ Cache.prototype._getKeyIndexForMany = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForSingle | ||
* | ||
* @description | ||
* gets the matching key index when a single key is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a single key is used. | ||
*/ | ||
@@ -269,10 +259,3 @@ Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
/** | ||
* @function orderByLru | ||
* | ||
* @description | ||
* order the array based on a Least-Recently-Used basis | ||
* | ||
* @param key the new key to move to the front | ||
* @param value the new value to move to the front | ||
* @param startingIndex the index of the item to move to the front | ||
* Order the array based on a Least-Recently-Used basis. | ||
*/ | ||
@@ -301,9 +284,4 @@ Cache.prototype.orderByLru = function (key, value, startingIndex) { | ||
/** | ||
* @function updateAsyncCache | ||
* | ||
* @description | ||
* update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed | ||
* | ||
* @param memoized the memoized function | ||
* Update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed. | ||
*/ | ||
@@ -335,3 +313,2 @@ Cache.prototype.updateAsyncCache = function (memoized) { | ||
// cache | ||
function createMemoizedFunction(fn, options) { | ||
@@ -338,0 +315,0 @@ if (options === void 0) { options = {}; } |
@@ -82,3 +82,3 @@ (function (global, factory) { | ||
function isMemoized(fn) { | ||
return (typeof fn === 'function' && fn.isMemoized); | ||
return typeof fn === 'function' && fn.isMemoized; | ||
} | ||
@@ -145,2 +145,5 @@ /** | ||
Object.defineProperty(Cache.prototype, "size", { | ||
/** | ||
* The number of cached [key,value] results. | ||
*/ | ||
get: function () { | ||
@@ -153,2 +156,7 @@ return this.keys.length; | ||
Object.defineProperty(Cache.prototype, "snapshot", { | ||
/** | ||
* A copy of the cache at a moment in time. This is useful | ||
* to compare changes over time, since the cache mutates | ||
* internally for performance reasons. | ||
*/ | ||
get: function () { | ||
@@ -165,9 +173,3 @@ return { | ||
/** | ||
* @function _getKeyIndexFromMatchingKey | ||
* | ||
* @description | ||
* gets the matching key index when a custom key matcher is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a custom key matcher is used. | ||
*/ | ||
@@ -194,9 +196,3 @@ Cache.prototype._getKeyIndexFromMatchingKey = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForMany | ||
* | ||
* @description | ||
* gets the matching key index when multiple keys are used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when multiple keys are used. | ||
*/ | ||
@@ -244,9 +240,3 @@ Cache.prototype._getKeyIndexForMany = function (keyToMatch) { | ||
/** | ||
* @function _getKeyIndexForSingle | ||
* | ||
* @description | ||
* gets the matching key index when a single key is used | ||
* | ||
* @param keyToMatch the key to match | ||
* @returns the index of the matching key, or -1 | ||
* Gets the matching key index when a single key is used. | ||
*/ | ||
@@ -275,10 +265,3 @@ Cache.prototype._getKeyIndexForSingle = function (keyToMatch) { | ||
/** | ||
* @function orderByLru | ||
* | ||
* @description | ||
* order the array based on a Least-Recently-Used basis | ||
* | ||
* @param key the new key to move to the front | ||
* @param value the new value to move to the front | ||
* @param startingIndex the index of the item to move to the front | ||
* Order the array based on a Least-Recently-Used basis. | ||
*/ | ||
@@ -307,9 +290,4 @@ Cache.prototype.orderByLru = function (key, value, startingIndex) { | ||
/** | ||
* @function updateAsyncCache | ||
* | ||
* @description | ||
* update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed | ||
* | ||
* @param memoized the memoized function | ||
* Update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed. | ||
*/ | ||
@@ -341,3 +319,2 @@ Cache.prototype.updateAsyncCache = function (memoized) { | ||
// cache | ||
function createMemoizedFunction(fn, options) { | ||
@@ -344,0 +321,0 @@ if (options === void 0) { options = {}; } |
157
index.d.ts
@@ -1,61 +0,132 @@ | ||
type Dictionary<Type> = { | ||
export interface Dictionary<Type> { | ||
[key: string]: Type; | ||
[index: number]: Type; | ||
}; | ||
} | ||
type AnyFn = (...args: any[]) => any; | ||
export type AnyFn = (...args: any[]) => any; | ||
export declare namespace MicroMemoize { | ||
export type Key = any[]; | ||
export type Value = any; | ||
export type Key = any[]; | ||
export type RawKey = Key | IArguments; | ||
export type Value = any; | ||
export type RawKey = Key | IArguments; | ||
export interface CacheSnapshot { | ||
keys: Key[]; | ||
size: number; | ||
values: Value[]; | ||
} | ||
export type Cache<Fn extends AnyFn = AnyFn> = import('./src/Cache').Cache<Fn>; | ||
export class Cache<Fn extends AnyFn> { | ||
readonly canTransformKey: boolean; | ||
readonly getKeyIndex: KeyIndexGetter; | ||
readonly options: NormalizedOptions<Fn>; | ||
readonly shouldCloneArguments: boolean; | ||
readonly shouldUpdateOnAdd: boolean; | ||
readonly shouldUpdateOnChange: boolean; | ||
readonly shouldUpdateOnHit: boolean; | ||
export type EqualityComparator = (object1: any, object2: any) => boolean; | ||
/** | ||
* The prevents call arguments which have cached results. | ||
*/ | ||
keys: Key[]; | ||
/** | ||
* The results of previous cached calls. | ||
*/ | ||
values: Value[]; | ||
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean; | ||
constructor(options: NormalizedOptions<Fn>); | ||
export type CacheModifiedHandler<Fn extends AnyFn = AnyFn> = ( | ||
cache: Cache<Fn>, | ||
options: NormalizedOptions<Fn>, | ||
memoized: Fn, | ||
) => void; | ||
/** | ||
* The number of cached [key,value] results. | ||
*/ | ||
get size(): number; | ||
export type KeyTransformer = (args: Key) => Key; | ||
/** | ||
* A copy of the cache at a moment in time. This is useful | ||
* to compare changes over time, since the cache mutates | ||
* internally for performance reasons. | ||
*/ | ||
get snapshot(): CacheSnapshot; | ||
export type KeyIndexGetter = (keyToMatch: RawKey) => number; | ||
/** | ||
* Order the array based on a Least-Recently-Used basis. | ||
*/ | ||
orderByLru(key: Key, value: Value, startingIndex: number): void; | ||
export type StandardOptions<Fn extends AnyFn = AnyFn> = { | ||
isEqual?: EqualityComparator; | ||
isMatchingKey?: MatchingKeyComparator; | ||
isPromise?: boolean; | ||
maxSize?: number; | ||
onCacheAdd?: CacheModifiedHandler<Fn>; | ||
onCacheChange?: CacheModifiedHandler<Fn>; | ||
onCacheHit?: CacheModifiedHandler<Fn>; | ||
transformKey?: KeyTransformer; | ||
/** | ||
* Update the promise method to auto-remove from cache if rejected, and | ||
* if resolved then fire cache hit / changed. | ||
*/ | ||
updateAsyncCache(memoized: Memoized<Fn>): void; | ||
} | ||
export type EqualityComparator = (object1: any, object2: any) => boolean; | ||
export type MatchingKeyComparator = (key1: Key, key2: RawKey) => boolean; | ||
export type CacheModifiedHandler<Fn extends AnyFn> = ( | ||
cache: Cache<Fn>, | ||
options: NormalizedOptions<Fn>, | ||
memoized: Memoized<Fn>, | ||
) => void; | ||
export type KeyTransformer = (args: Key) => Key; | ||
export type KeyIndexGetter = (keyToMatch: RawKey) => number; | ||
export interface StandardOptions<Fn extends AnyFn> { | ||
isEqual?: EqualityComparator; | ||
isMatchingKey?: MatchingKeyComparator; | ||
isPromise?: boolean; | ||
maxSize?: number; | ||
onCacheAdd?: CacheModifiedHandler<Fn>; | ||
onCacheChange?: CacheModifiedHandler<Fn>; | ||
onCacheHit?: CacheModifiedHandler<Fn>; | ||
transformKey?: KeyTransformer; | ||
} | ||
export interface Options<Fn extends AnyFn> | ||
extends StandardOptions<Fn>, | ||
Dictionary<any> {} | ||
export interface NormalizedOptions<Fn extends AnyFn> extends Options<Fn> { | ||
isEqual: EqualityComparator; | ||
isPromise: boolean; | ||
maxSize: number; | ||
} | ||
export type Memoized<Fn extends AnyFn> = Fn & | ||
Dictionary<any> & { | ||
cache: Cache<Fn>; | ||
fn: Fn; | ||
isMemoized: true; | ||
options: NormalizedOptions<Fn>; | ||
}; | ||
export type Options<Fn extends AnyFn = AnyFn> = StandardOptions<Fn> & | ||
Dictionary<any>; | ||
export type NormalizedOptions<Fn extends AnyFn = AnyFn> = Options<Fn> & { | ||
isEqual: EqualityComparator; | ||
isPromise: boolean; | ||
maxSize: number; | ||
/** | ||
* @deprecated | ||
* All types declared on this namespace are available for direct import | ||
* from the package. In a future release, this namespace will be removed | ||
* in favor of those direct imports. | ||
*/ | ||
export declare namespace MicroMemoize { | ||
export type { | ||
Cache, | ||
CacheModifiedHandler, | ||
EqualityComparator, | ||
Key, | ||
KeyIndexGetter, | ||
KeyTransformer, | ||
MatchingKeyComparator, | ||
Memoized, | ||
NormalizedOptions, | ||
Options, | ||
StandardOptions, | ||
RawKey, | ||
Value, | ||
}; | ||
export type Memoized<Fn extends AnyFn = AnyFn> = Fn & | ||
Dictionary<any> & { | ||
cache: Cache<Fn>; | ||
fn: Fn; | ||
isMemoized: true; | ||
options: NormalizedOptions<Fn>; | ||
}; | ||
} | ||
export default function memoize<Fn extends AnyFn>( | ||
fn: Fn | MicroMemoize.Memoized<Fn>, | ||
options?: MicroMemoize.Options<Fn>, | ||
): MicroMemoize.Memoized<Fn>; | ||
fn: Fn | Memoized<Fn>, | ||
options?: Options<Fn>, | ||
): Memoized<Fn>; |
@@ -17,8 +17,9 @@ { | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^10.0.1", | ||
"@rollup/plugin-terser": "^0.4.1", | ||
"@rollup/plugin-typescript": "^11.1.0", | ||
"@types/bluebird": "^3.5.38", | ||
"@types/jest": "^29.2.3", | ||
"@types/react": "^18.0.26", | ||
"@typescript-eslint/eslint-plugin": "^5.45.0", | ||
"@typescript-eslint/parser": "^5.45.0", | ||
"@types/jest": "^29.5.1", | ||
"@types/react": "^18.2.6", | ||
"@typescript-eslint/eslint-plugin": "^5.59.2", | ||
"@typescript-eslint/parser": "^5.59.2", | ||
"benchee": "^1.1.0", | ||
@@ -28,11 +29,11 @@ "benchmark": "^2.1.4", | ||
"cli-table2": "^0.2.0", | ||
"eslint": "^8.29.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-react": "^7.31.11", | ||
"eslint-webpack-plugin": "^3.2.0", | ||
"fast-equals": "^4.0.3", | ||
"eslint": "^8.40.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-react": "^7.32.2", | ||
"eslint-webpack-plugin": "^4.0.1", | ||
"fast-equals": "^5.0.1", | ||
"fast-memoize": "^2.5.2", | ||
"html-webpack-plugin": "^5.5.0", | ||
"html-webpack-plugin": "^5.5.1", | ||
"in-publish": "^2.0.1", | ||
"jest": "^29.3.1", | ||
"jest": "^29.5.0", | ||
"lodash": "^4.17.21", | ||
@@ -44,19 +45,18 @@ "lru-memoize": "^1.1.0", | ||
"mini-bench": "^1.0.0", | ||
"ora": "^6.1.2", | ||
"ora": "^6.3.0", | ||
"performance-now": "^2.1.0", | ||
"ramda": "^0.28.0", | ||
"ramda": "^0.29.0", | ||
"react": "^18.2.0", | ||
"release-it": "^15.5.1", | ||
"rollup": "^3.5.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"release-it": "^15.10.3", | ||
"rollup": "^3.21.5", | ||
"rsvp": "^4.8.5", | ||
"simple-statistics": "^7.8.0", | ||
"ts-jest": "^29.0.3", | ||
"simple-statistics": "^7.8.3", | ||
"ts-jest": "^29.1.0", | ||
"ts-loader": "^9.4.2", | ||
"tslib": "^2.4.1", | ||
"typescript": "^4.9.3", | ||
"tslib": "^2.5.0", | ||
"typescript": "^5.0.4", | ||
"underscore": "^1.13.6", | ||
"webpack": "^5.75.0", | ||
"webpack-cli": "^5.0.0", | ||
"webpack-dev-server": "^4.11.1" | ||
"webpack": "^5.82.0", | ||
"webpack-cli": "^5.1.0", | ||
"webpack-dev-server": "^4.15.0" | ||
}, | ||
@@ -97,3 +97,3 @@ "homepage": "https://github.com/planttheidea/micro-memoize#readme", | ||
"prepublish": "if in-publish; then npm run prepublish:compile; fi", | ||
"prepublish:compile": "npm run lint && npm run test:coverage && npm run dist", | ||
"prepublish:compile": "npm run typecheck && npm run lint && npm run test:coverage && npm run dist", | ||
"release": "release-it", | ||
@@ -104,7 +104,8 @@ "release:beta": "release-it --config=.release-it.beta.json", | ||
"test:coverage": "npm run test -- --coverage", | ||
"test:watch": "npm run test -- --watch" | ||
"test:watch": "npm run test -- --watch", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"sideEffects": false, | ||
"types": "./index.d.ts", | ||
"version": "4.0.14" | ||
"version": "4.1.2" | ||
} |
@@ -40,3 +40,3 @@ # micro-memoize | ||
As the author of [`moize`](https://github.com/planttheidea/moize), I created a consistently fast memoization library, but `moize` has a lot of features to satisfy a large number of edge cases. `micro-memoize` is a simpler approach, focusing on the core feature set with a much smaller footprint (~1.5kB minified+gzipped). Stripping out these edge cases also allows `micro-memoize` to be faster across the board than `moize`. | ||
As the author of [`moize`](https://github.com/planttheidea/moize), I created a consistently fast memoization library, but `moize` has a lot of features to satisfy a large number of edge cases. `micro-memoize` is a simpler approach, focusing on the core feature set with a much smaller footprint (~1.44kB minified+gzipped). Stripping out these edge cases also allows `micro-memoize` to be faster across the board than `moize`. | ||
@@ -43,0 +43,0 @@ ## Importing |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
132236
15
1613