@thi.ng/memoize
Advanced tools
Comparing version 3.1.65 to 3.2.0
# Change Log | ||
- **Last updated**: 2024-03-13T14:04:31Z | ||
- **Last updated**: 2024-03-27T09:53:45Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,16 @@ | ||
## [3.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.2.0) (2024-03-27) | ||
#### 🚀 Features | ||
- add memoizeO() ([af2ead9](https://github.com/thi-ng/umbrella/commit/af2ead9)) | ||
- add memoize2/3/4O() ([8309236](https://github.com/thi-ng/umbrella/commit/8309236)) | ||
#### ♻️ Refactoring | ||
- minor updates, use plain objects where possible ([f44be23](https://github.com/thi-ng/umbrella/commit/f44be23)) | ||
- update defOnce() & memoizeJ() to use Object.create(null) as default store | ||
- update default args in others | ||
- update docs | ||
### [3.1.41](https://github.com/thi-ng/umbrella/tree/@thi.ng/memoize@3.1.41) (2023-11-09) | ||
@@ -14,0 +28,0 @@ |
import type { Fn0 } from "@thi.ng/api"; | ||
/** | ||
* Lightweight named singleton factory, intended for hot-module | ||
* replacement situations. Takes a (preferably globally unique) `id` and | ||
* `factory` function. If there's no value defined for `id` yet, calls | ||
* `factory` to produce the singleton value and caches it. Returns | ||
* singleton value. | ||
* Lightweight named singleton factory, intended for hot-module replacement | ||
* situations. Takes a (preferably globally unique) `id` and `factory` function. | ||
* If there's no value defined for `id` yet, calls `factory` to produce the | ||
* singleton value and caches it. Returns singleton value. | ||
* | ||
* Note: All created values will remain in the private cache until the | ||
* JS process terminates or this module itself has been reloaded (though | ||
* the latter shouldn't happen in an HMR workflow). | ||
* @remarks | ||
* Note: All created values will remain in the private cache until the JS | ||
* process terminates or this module itself has been reloaded (though the latter | ||
* shouldn't happen in an HMR workflow). | ||
* | ||
* For more control over memory usage, consider using other memoize functions in | ||
* this package with one of the available cache implementations from | ||
* [thi.ng/cache](https://thi.ng/cache). | ||
* | ||
* @param id - | ||
@@ -14,0 +18,0 @@ * @param factory - |
@@ -1,5 +0,5 @@ | ||
const cache = {}; | ||
const defonce = (id, factory) => cache.hasOwnProperty(id) ? cache[id] : cache[id] = factory(); | ||
const cache = /* @__PURE__ */ Object.create(null); | ||
const defonce = (id, factory) => id in cache ? cache[id] : cache[id] = factory(); | ||
export { | ||
defonce | ||
}; |
@@ -1,9 +0,6 @@ | ||
const doOnce = (fn, cache) => { | ||
!cache && (cache = /* @__PURE__ */ new Map()); | ||
return (x) => { | ||
if (!cache.has(x)) { | ||
cache.set(x, true); | ||
fn(x); | ||
} | ||
}; | ||
const doOnce = (fn, cache = /* @__PURE__ */ new Map()) => (x) => { | ||
if (!cache.has(x)) { | ||
cache.set(x, true); | ||
fn(x); | ||
} | ||
}; | ||
@@ -10,0 +7,0 @@ export { |
@@ -7,2 +7,3 @@ export * from "./api.js"; | ||
export * from "./memoizej.js"; | ||
export * from "./memoizeo.js"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -7,1 +7,2 @@ export * from "./api.js"; | ||
export * from "./memoizej.js"; | ||
export * from "./memoizeo.js"; |
@@ -9,7 +9,10 @@ import type { Fn, Fn2, Fn3, Fn4 } from "@thi.ng/api"; | ||
* | ||
* @remarks | ||
* **Important:** It only makes sense to use `Map` types which support value | ||
* (rather than object) equality, e.g. those provided by | ||
* [`thi.ng/associative`](https://thi.ng/associative). Using a native `Map` type | ||
* here will lead to memory leaks! Alternatively, use {@link memoizeJ}. | ||
* here will lead to memory leaks! | ||
* | ||
* Also see {@link memoizeJ}, {@link memoize1}, {@link memoizeO}. | ||
* | ||
* @param fn - | ||
@@ -16,0 +19,0 @@ * @param cache - |
import type { Fn } from "@thi.ng/api"; | ||
import type { MapLike } from "./api.js"; | ||
/** | ||
* Optimized memoization for single arg functions. If the function expects args | ||
* other than strings or numbers, you MUST provide a `Map` implementation which | ||
* supports value (rather than object) equality, e.g. one of those provided by | ||
* Optimized memoization for single arg functions. | ||
* | ||
* @remarks | ||
* If the function expects args other than strings or numbers, you MUST provide | ||
* a `Map` implementation which supports value (rather than object) equality, | ||
* e.g. one of those provided by | ||
* [`thi.ng/associative`](https://thi.ng/associative). Using a native `Map` type | ||
* here will lead to memory leaks! Alternatively, use {@link memoizeJ}. | ||
* here will lead to memory leaks! | ||
* | ||
* Also see {@link memoizeO}, {@link memoizeJ}, {@link memoize}. | ||
* | ||
* @param fn - | ||
@@ -11,0 +16,0 @@ * @param cache - |
@@ -1,7 +0,4 @@ | ||
const memoize1 = (fn, cache) => { | ||
!cache && (cache = /* @__PURE__ */ new Map()); | ||
return (x) => { | ||
let res; | ||
return cache.has(x) ? cache.get(x) : (cache.set(x, res = fn(x)), res); | ||
}; | ||
const memoize1 = (fn, cache = /* @__PURE__ */ new Map()) => (x) => { | ||
let res; | ||
return cache.has(x) ? cache.get(x) : (cache.set(x, res = fn(x)), res); | ||
}; | ||
@@ -8,0 +5,0 @@ export { |
@@ -8,5 +8,8 @@ import type { Fn, Fn2, Fn3, Fn4, IObjectOf } from "@thi.ng/api"; | ||
* | ||
* @remarks | ||
* **Important:** If the given args cannot be stringified, the user | ||
* function will ALWAYS be called (without caching result). | ||
* | ||
* Also see {@link memoize}, {@link memoize1}, {@link memoizeO}. | ||
* | ||
* @param fn - | ||
@@ -13,0 +16,0 @@ * @param cache - |
@@ -1,3 +0,2 @@ | ||
function memoizeJ(fn, cache) { | ||
!cache && (cache = {}); | ||
function memoizeJ(fn, cache = /* @__PURE__ */ Object.create(null)) { | ||
return (...args) => { | ||
@@ -4,0 +3,0 @@ const key = JSON.stringify(args); |
{ | ||
"name": "@thi.ng/memoize", | ||
"version": "3.1.65", | ||
"version": "3.2.0", | ||
"description": "Function memoization with configurable caching", | ||
@@ -39,10 +39,10 @@ "type": "module", | ||
"dependencies": { | ||
"@thi.ng/api": "^8.9.30" | ||
"@thi.ng/api": "^8.9.31" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.42.3", | ||
"esbuild": "^0.20.1", | ||
"@microsoft/api-extractor": "^7.43.0", | ||
"esbuild": "^0.20.2", | ||
"rimraf": "^5.0.5", | ||
"typedoc": "^0.25.12", | ||
"typescript": "^5.4.2" | ||
"typescript": "^5.4.3" | ||
}, | ||
@@ -86,2 +86,5 @@ "keywords": [ | ||
"default": "./memoizej.js" | ||
}, | ||
"./memoizeo": { | ||
"default": "./memoizeo.js" | ||
} | ||
@@ -95,3 +98,3 @@ }, | ||
}, | ||
"gitHead": "7f3fcbd6c0462b0ce45afa141fe163d1f297fd51\n" | ||
"gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n" | ||
} |
@@ -75,3 +75,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 247 bytes | ||
Package sizes (brotli'd, pre-treeshake): ESM: 334 bytes | ||
@@ -78,0 +78,0 @@ ## Dependencies |
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
34395
20
223
Updated@thi.ng/api@^8.9.31