@thi.ng/memoize 3.1.65 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/README.md +1 -1
- package/defonce.d.ts +12 -8
- package/defonce.js +2 -2
- package/do-once.js +5 -8
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/memoize.d.ts +4 -1
- package/memoize1.d.ts +9 -4
- package/memoize1.js +3 -6
- package/memoizej.d.ts +3 -0
- package/memoizej.js +1 -2
- package/memoizeo.d.ts +53 -0
- package/memoizeo.js +19 -0
- package/package.json +9 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-03-
|
|
3
|
+
- **Last updated**: 2024-03-27T09:53:45Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,20 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [3.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.2.0) (2024-03-27)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add memoizeO() ([af2ead9](https://github.com/thi-ng/umbrella/commit/af2ead9))
|
|
17
|
+
- add memoize2/3/4O() ([8309236](https://github.com/thi-ng/umbrella/commit/8309236))
|
|
18
|
+
|
|
19
|
+
#### ♻️ Refactoring
|
|
20
|
+
|
|
21
|
+
- minor updates, use plain objects where possible ([f44be23](https://github.com/thi-ng/umbrella/commit/f44be23))
|
|
22
|
+
- update defOnce() & memoizeJ() to use Object.create(null) as default store
|
|
23
|
+
- update default args in others
|
|
24
|
+
- update docs
|
|
25
|
+
|
|
12
26
|
### [3.1.41](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.1.41) (2023-11-09)
|
|
13
27
|
|
|
14
28
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
package/defonce.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import type { Fn0 } from "@thi.ng/api";
|
|
2
2
|
/**
|
|
3
|
-
* Lightweight named singleton factory, intended for hot-module
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* singleton value.
|
|
3
|
+
* Lightweight named singleton factory, intended for hot-module replacement
|
|
4
|
+
* situations. Takes a (preferably globally unique) `id` and `factory` function.
|
|
5
|
+
* If there's no value defined for `id` yet, calls `factory` to produce the
|
|
6
|
+
* singleton value and caches it. Returns singleton value.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Note: All created values will remain in the private cache until the JS
|
|
10
|
+
* process terminates or this module itself has been reloaded (though the latter
|
|
11
|
+
* shouldn't happen in an HMR workflow).
|
|
12
|
+
*
|
|
13
|
+
* For more control over memory usage, consider using other memoize functions in
|
|
14
|
+
* this package with one of the available cache implementations from
|
|
15
|
+
* [thi.ng/cache](https://thi.ng/cache).
|
|
12
16
|
*
|
|
13
17
|
* @param id -
|
|
14
18
|
* @param factory -
|
package/defonce.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const cache =
|
|
2
|
-
const defonce = (id, factory) =>
|
|
1
|
+
const cache = /* @__PURE__ */ Object.create(null);
|
|
2
|
+
const defonce = (id, factory) => id in cache ? cache[id] : cache[id] = factory();
|
|
3
3
|
export {
|
|
4
4
|
defonce
|
|
5
5
|
};
|
package/do-once.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
const doOnce = (fn, cache) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
fn(x);
|
|
7
|
-
}
|
|
8
|
-
};
|
|
1
|
+
const doOnce = (fn, cache = /* @__PURE__ */ new Map()) => (x) => {
|
|
2
|
+
if (!cache.has(x)) {
|
|
3
|
+
cache.set(x, true);
|
|
4
|
+
fn(x);
|
|
5
|
+
}
|
|
9
6
|
};
|
|
10
7
|
export {
|
|
11
8
|
doOnce
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/memoize.d.ts
CHANGED
|
@@ -6,10 +6,13 @@ import type { MapLike } from "./api.js";
|
|
|
6
6
|
* memoized result of given args. Supports generics for up to 4 args (otherwise
|
|
7
7
|
* untyped).
|
|
8
8
|
*
|
|
9
|
+
* @remarks
|
|
9
10
|
* **Important:** It only makes sense to use `Map` types which support value
|
|
10
11
|
* (rather than object) equality, e.g. those provided by
|
|
11
12
|
* [`thi.ng/associative`](https://thi.ng/associative). Using a native `Map` type
|
|
12
|
-
* here will lead to memory leaks!
|
|
13
|
+
* here will lead to memory leaks!
|
|
14
|
+
*
|
|
15
|
+
* Also see {@link memoizeJ}, {@link memoize1}, {@link memoizeO}.
|
|
13
16
|
*
|
|
14
17
|
* @param fn -
|
|
15
18
|
* @param cache -
|
package/memoize1.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import type { Fn } from "@thi.ng/api";
|
|
2
2
|
import type { MapLike } from "./api.js";
|
|
3
3
|
/**
|
|
4
|
-
* Optimized memoization for single arg functions.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Optimized memoization for single arg functions.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* If the function expects args other than strings or numbers, you MUST provide
|
|
8
|
+
* a `Map` implementation which supports value (rather than object) equality,
|
|
9
|
+
* e.g. one of those provided by
|
|
7
10
|
* [`thi.ng/associative`](https://thi.ng/associative). Using a native `Map` type
|
|
8
|
-
* here will lead to memory leaks!
|
|
11
|
+
* here will lead to memory leaks!
|
|
12
|
+
*
|
|
13
|
+
* Also see {@link memoizeO}, {@link memoizeJ}, {@link memoize}.
|
|
9
14
|
*
|
|
10
15
|
* @param fn -
|
|
11
16
|
* @param cache -
|
package/memoize1.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
const memoize1 = (fn, cache) => {
|
|
2
|
-
|
|
3
|
-
return (x)
|
|
4
|
-
let res;
|
|
5
|
-
return cache.has(x) ? cache.get(x) : (cache.set(x, res = fn(x)), res);
|
|
6
|
-
};
|
|
1
|
+
const memoize1 = (fn, cache = /* @__PURE__ */ new Map()) => (x) => {
|
|
2
|
+
let res;
|
|
3
|
+
return cache.has(x) ? cache.get(x) : (cache.set(x, res = fn(x)), res);
|
|
7
4
|
};
|
|
8
5
|
export {
|
|
9
6
|
memoize1
|
package/memoizej.d.ts
CHANGED
|
@@ -5,9 +5,12 @@ import type { Fn, Fn2, Fn3, Fn4, IObjectOf } from "@thi.ng/api";
|
|
|
5
5
|
* memoized result for given args. Supports generics for up to 4 args
|
|
6
6
|
* (otherwise untyped).
|
|
7
7
|
*
|
|
8
|
+
* @remarks
|
|
8
9
|
* **Important:** If the given args cannot be stringified, the user
|
|
9
10
|
* function will ALWAYS be called (without caching result).
|
|
10
11
|
*
|
|
12
|
+
* Also see {@link memoize}, {@link memoize1}, {@link memoizeO}.
|
|
13
|
+
*
|
|
11
14
|
* @param fn -
|
|
12
15
|
* @param cache -
|
|
13
16
|
*/
|
package/memoizej.js
CHANGED
package/memoizeo.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Fn, Fn2, Fn3, Fn4, NumOrString } from "@thi.ng/api";
|
|
2
|
+
/**
|
|
3
|
+
* The most minimalistic & fastest memoization function of this package. Similar
|
|
4
|
+
* to {@link memoize1}, but only supports numbers or strings as keys and uses a
|
|
5
|
+
* vanilla JS object as cache.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Also see {@link memoize1}, {@link memoizeJ}, {@link memoize}.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts tangle:../../export/memoizeo.ts
|
|
12
|
+
* import { memoizeO } from "@thi.ng/memoize";
|
|
13
|
+
*
|
|
14
|
+
* const test = memoizeO((x: number) => (console.log("exec", x), x * 10));
|
|
15
|
+
*
|
|
16
|
+
* console.log(test(1));
|
|
17
|
+
* // exec 1
|
|
18
|
+
* // 10
|
|
19
|
+
*
|
|
20
|
+
* console.log(test(1))
|
|
21
|
+
* // 10
|
|
22
|
+
*
|
|
23
|
+
* console.log(test(2));
|
|
24
|
+
* // exec 2
|
|
25
|
+
* // 20
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param fn
|
|
29
|
+
* @param cache
|
|
30
|
+
*/
|
|
31
|
+
export declare const memoizeO: <A extends NumOrString, B>(fn: Fn<A, B>, cache?: Record<NumOrString, B>) => (x: A) => B;
|
|
32
|
+
/**
|
|
33
|
+
* Like {@link memoizeO}, but for functions with 2 arguments.
|
|
34
|
+
*
|
|
35
|
+
* @param fn
|
|
36
|
+
* @param cache
|
|
37
|
+
*/
|
|
38
|
+
export declare const memoize2O: <A extends NumOrString, B extends NumOrString, C>(fn: Fn2<A, B, C>, cache?: Record<string, C>) => (a: A, b: B) => C;
|
|
39
|
+
/**
|
|
40
|
+
* Like {@link memoizeO}, but for functions with 3 arguments.
|
|
41
|
+
*
|
|
42
|
+
* @param fn
|
|
43
|
+
* @param cache
|
|
44
|
+
*/
|
|
45
|
+
export declare const memoize3O: <A extends NumOrString, B extends NumOrString, C extends NumOrString, D>(fn: Fn3<A, B, C, D>, cache?: Record<string, D>) => (a: A, b: B, c: C) => D;
|
|
46
|
+
/**
|
|
47
|
+
* Like {@link memoizeO}, but for functions with 4 arguments.
|
|
48
|
+
*
|
|
49
|
+
* @param fn
|
|
50
|
+
* @param cache
|
|
51
|
+
*/
|
|
52
|
+
export declare const memoize4O: <A extends NumOrString, B extends NumOrString, C extends NumOrString, D extends NumOrString, E>(fn: Fn4<A, B, C, D, E>, cache?: Record<string, E>) => (a: A, b: B, c: C, d: D) => E;
|
|
53
|
+
//# sourceMappingURL=memoizeo.d.ts.map
|
package/memoizeo.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const memoizeO = (fn, cache = /* @__PURE__ */ Object.create(null)) => (x) => x in cache ? cache[x] : cache[x] = fn(x);
|
|
2
|
+
const memoize2O = (fn, cache = /* @__PURE__ */ Object.create(null)) => (a, b) => {
|
|
3
|
+
const key = a + "-" + b;
|
|
4
|
+
return key in cache ? cache[key] : cache[key] = fn(a, b);
|
|
5
|
+
};
|
|
6
|
+
const memoize3O = (fn, cache = /* @__PURE__ */ Object.create(null)) => (a, b, c) => {
|
|
7
|
+
const key = a + "-" + b + "-" + c;
|
|
8
|
+
return key in cache ? cache[key] : cache[key] = fn(a, b, c);
|
|
9
|
+
};
|
|
10
|
+
const memoize4O = (fn, cache = /* @__PURE__ */ Object.create(null)) => (a, b, c, d) => {
|
|
11
|
+
const key = a + "-" + b + "-" + c + "-" + d;
|
|
12
|
+
return key in cache ? cache[key] : cache[key] = fn(a, b, c, d);
|
|
13
|
+
};
|
|
14
|
+
export {
|
|
15
|
+
memoize2O,
|
|
16
|
+
memoize3O,
|
|
17
|
+
memoize4O,
|
|
18
|
+
memoizeO
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/memoize",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Function memoization with configurable caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -36,14 +36,14 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.9.
|
|
39
|
+
"@thi.ng/api": "^8.9.31"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@microsoft/api-extractor": "^7.
|
|
43
|
-
"esbuild": "^0.20.
|
|
42
|
+
"@microsoft/api-extractor": "^7.43.0",
|
|
43
|
+
"esbuild": "^0.20.2",
|
|
44
44
|
"rimraf": "^5.0.5",
|
|
45
45
|
"typedoc": "^0.25.12",
|
|
46
|
-
"typescript": "^5.4.
|
|
46
|
+
"typescript": "^5.4.3"
|
|
47
47
|
},
|
|
48
48
|
"keywords": [
|
|
49
49
|
"cache",
|
|
@@ -82,6 +82,9 @@
|
|
|
82
82
|
},
|
|
83
83
|
"./memoizej": {
|
|
84
84
|
"default": "./memoizej.js"
|
|
85
|
+
},
|
|
86
|
+
"./memoizeo": {
|
|
87
|
+
"default": "./memoizeo.js"
|
|
85
88
|
}
|
|
86
89
|
},
|
|
87
90
|
"thi.ng": {
|
|
@@ -90,5 +93,5 @@
|
|
|
90
93
|
],
|
|
91
94
|
"year": 2018
|
|
92
95
|
},
|
|
93
|
-
"gitHead": "
|
|
96
|
+
"gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n"
|
|
94
97
|
}
|