react-auto-memo-z 1.0.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/LICENSE +21 -0
- package/README.md +110 -0
- package/build/index.cjs.js +1 -0
- package/build/index.d.ts +6 -0
- package/build/index.esm.js +1 -0
- package/build/memo/autoMemo.d.ts +5 -0
- package/build/memo/shallowEqual.d.ts +1 -0
- package/build/memo/useAutoCallback.d.ts +1 -0
- package/build/memo/useAutoMemo.d.ts +5 -0
- package/build/memo/useCallbackOne.d.ts +1 -0
- package/build/memo/useMemoOne.d.ts +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Delpi.Kye
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
|
|
2
|
+
## β¨ react-auto-memo-z
|
|
3
|
+
|
|
4
|
+
[](https://www.npmjs.com/package/react-auto-memo-z)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Utilities avoids premature optimization while still protecting you from costly re-renders in real-world scenarios..
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
### π Why react-auto-memo-z?
|
|
13
|
+
|
|
14
|
+
β Memoization only when needed
|
|
15
|
+
β Zero config
|
|
16
|
+
β No dependency mistakes
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
### π¦ Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install react-auto-memo-z
|
|
24
|
+
# or
|
|
25
|
+
yarn add react-auto-memo-z
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### π Usage
|
|
31
|
+
|
|
32
|
+
#### Snippet
|
|
33
|
+
```tsx
|
|
34
|
+
import { autoMemo, useAutoMemo } from 'react-auto-memo-z';
|
|
35
|
+
|
|
36
|
+
type Props = {
|
|
37
|
+
data: number[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const HeavyCard = autoMemo(({ data }: Props) => {
|
|
41
|
+
const total = useAutoMemo(
|
|
42
|
+
() => {
|
|
43
|
+
// Simulate an expensive computation
|
|
44
|
+
let sum = 0;
|
|
45
|
+
for (let i = 0; i < data.length; i++) {
|
|
46
|
+
sum += data[i];
|
|
47
|
+
}
|
|
48
|
+
return sum;
|
|
49
|
+
},
|
|
50
|
+
[data]
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return <div>Total: {total}</div>;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
export default function App() {
|
|
57
|
+
return <HeavyCard data={[1, 2, 3]} />;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### What happens under the hood?
|
|
63
|
+
|
|
64
|
+
<b> Initial render (no optimization yet) </b>
|
|
65
|
+
|
|
66
|
+
- HeavyCard renders normally.
|
|
67
|
+
|
|
68
|
+
- useAutoMemo executes the factory function.
|
|
69
|
+
|
|
70
|
+
- The execution time is measured internally.
|
|
71
|
+
|
|
72
|
+
- If the computation is cheap, no memoization is applied.
|
|
73
|
+
|
|
74
|
+
<b> Detecting expensive work </b>
|
|
75
|
+
|
|
76
|
+
- On subsequent renders, if:
|
|
77
|
+
|
|
78
|
+
- the computation becomes slow, and the dependencies (data) remain referentially stable
|
|
79
|
+
|
|
80
|
+
- Then: useAutoMemo automatically switches to useMemo
|
|
81
|
+
|
|
82
|
+
- The computed value is now cached.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
### β¨ Why this approach is different
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
| Traditional | react-auto-memo |
|
|
91
|
+
| ------------------------ | ----------------------------- |
|
|
92
|
+
| Dev decides when to memo | Runtime decides automatically |
|
|
93
|
+
| Risk of over-memoization | Memo only when slow |
|
|
94
|
+
| Boilerplate everywhere | Clean, readable code |
|
|
95
|
+
| Hard to debug | Cost-based heuristics |
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
### π§ͺ When should you use it?
|
|
101
|
+
β
Dashboards with charts
|
|
102
|
+
β
Tables with frequent updates
|
|
103
|
+
β
Components receiving large objects
|
|
104
|
+
β
Performance-critical UI
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
### π License
|
|
109
|
+
|
|
110
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAutoMemo={},e.React)}(this,function(e,r){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(r),u=function(){return u=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var u in r=arguments[t])Object.prototype.hasOwnProperty.call(r,u)&&(e[u]=r[u]);return e},u.apply(this,arguments)};function o(e,r){if(Object.is(e,r))return!0;if("object"!=typeof e||"object"!=typeof r)return!1;var t=Object.keys(e),n=Object.keys(r);if(t.length!==n.length)return!1;for(var u=0,o=t;u<o.length;u++){var a=o[u];if(!Object.prototype.hasOwnProperty.call(r,a))return!1;if(!Object.is(e[a],r[a]))return!1}return!0}"function"==typeof SuppressedError&&SuppressedError,e.autoMemo=function(e,t){var a=void 0===t?{}:t,c=a.thresholdMs,f=void 0===c?8:c,i=a.disable;if(void 0!==i&&i)return e;var l=n.default.memo(e,o),s=function(t){var a=r.useRef(null),c=r.useRef(!1),i=performance.now();n.default.createElement(e,u({},t));var s=performance.now()-i;!c.current&&a.current&&o(a.current,t)&&s>f&&(c.current=!0,console.warn("[react-auto-memo] ".concat(e.name," is slow (").concat(s.toFixed(1),"ms)"))),a.current=t;var p=s>f?l:e;return n.default.createElement(p,u({},t))};return s.displayName="autoMemo(".concat(e.displayName||e.name,")"),s},e.useAutoCallback=function(e){var t=r.useRef(e);return t.current=e,r.useCallback(function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return t.current.apply(t,e)},[])},e.useAutoMemo=function(e,t,n){void 0===n&&(n={});var u=n.thresholdMs,o=void 0===u?1:u,a=r.useRef(!1),c=r.useMemo(e,t);if(!a.current){var f=performance.now();e(),performance.now()-f>o&&(a.current=!0)}return a.current?c:e()},e.useCallbackOne=function(e){var t=r.useRef(e);return t.current=e,r.useCallback(function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return t.current.apply(t,e)},[])},e.useMemoOne=function(e,t){var n=r.useRef(null);return n.current&&o(n.current.deps,t)||(n.current={deps:t,value:e()}),n.current.value},Object.defineProperty(e,"__esModule",{value:!0})});
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { autoMemo } from "./memo/autoMemo";
|
|
2
|
+
import { useAutoCallback } from "./memo/useAutoCallback";
|
|
3
|
+
import { useAutoMemo } from "./memo/useAutoMemo";
|
|
4
|
+
import { useCallbackOne } from "./memo/useCallbackOne";
|
|
5
|
+
import { useMemoOne } from "./memo/useMemoOne";
|
|
6
|
+
export { autoMemo, useAutoCallback, useAutoMemo, useMemoOne, useCallbackOne };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import r,{useRef as e,useCallback as n,useMemo as t}from"react";var o=function(){return o=Object.assign||function(r){for(var e,n=1,t=arguments.length;n<t;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r},o.apply(this,arguments)};function c(r,e){if(Object.is(r,e))return!0;if("object"!=typeof r||"object"!=typeof e)return!1;var n=Object.keys(r),t=Object.keys(e);if(n.length!==t.length)return!1;for(var o=0,c=n;o<c.length;o++){var u=c[o];if(!Object.prototype.hasOwnProperty.call(e,u))return!1;if(!Object.is(r[u],e[u]))return!1}return!0}function u(n,t){var u=void 0===t?{}:t,a=u.thresholdMs,i=void 0===a?8:a,f=u.disable;if(void 0!==f&&f)return n;var p=r.memo(n,c),l=function(t){var u=e(null),a=e(!1),f=performance.now();r.createElement(n,o({},t));var l=performance.now()-f;!a.current&&u.current&&c(u.current,t)&&l>i&&(a.current=!0,console.warn("[react-auto-memo] ".concat(n.name," is slow (").concat(l.toFixed(1),"ms)"))),u.current=t;var s=l>i?p:n;return r.createElement(s,o({},t))};return l.displayName="autoMemo(".concat(n.displayName||n.name,")"),l}function a(r){var t=e(r);return t.current=r,n(function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.current.apply(t,r)},[])}function i(r,n,o){void 0===o&&(o={});var c=o.thresholdMs,u=void 0===c?1:c,a=e(!1),i=t(r,n);if(!a.current){var f=performance.now();r(),performance.now()-f>u&&(a.current=!0)}return a.current?i:r()}function f(r){var t=e(r);return t.current=r,n(function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.current.apply(t,r)},[])}function p(r,n){var t=e(null);return t.current&&c(t.current.deps,n)||(t.current={deps:n,value:r()}),t.current.value}"function"==typeof SuppressedError&&SuppressedError;export{u as autoMemo,a as useAutoCallback,i as useAutoMemo,f as useCallbackOne,p as useMemoOne};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function shallowEqual(a: any, b: any): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useAutoCallback<T extends (...args: any[]) => any>(fn: T): T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useCallbackOne<T extends (...args: any[]) => any>(fn: T): T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useMemoOne<T>(factory: () => T, deps: any[]): T;
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-auto-memo-z",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Smart memoization utilities for React that apply optimization only when itβs worth it.",
|
|
5
|
+
"author": "Delpi.Kye",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
|
|
8
|
+
"homepage": "https://github.com/delpikye-v/react-auto-memo-z#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/delpikye-v/react-auto-memo-z.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/delpikye-v/react-auto-memo-z/issues"
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react",
|
|
19
|
+
"memo",
|
|
20
|
+
"use-memo",
|
|
21
|
+
"use-callback",
|
|
22
|
+
"react-hooks",
|
|
23
|
+
"performance",
|
|
24
|
+
"render-optimization",
|
|
25
|
+
"smart-memo",
|
|
26
|
+
"react-utils"
|
|
27
|
+
],
|
|
28
|
+
|
|
29
|
+
"main": "build/index.cjs.js",
|
|
30
|
+
"module": "build/index.esm.js",
|
|
31
|
+
"types": "build/index.d.ts",
|
|
32
|
+
|
|
33
|
+
"files": [
|
|
34
|
+
"build"
|
|
35
|
+
],
|
|
36
|
+
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
|
|
39
|
+
"scripts": {
|
|
40
|
+
"clean": "rimraf build",
|
|
41
|
+
"build": "rollup -c",
|
|
42
|
+
"dev": "rollup -c -w",
|
|
43
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react": ">=17"
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@rollup/plugin-commonjs": "^17.1.0",
|
|
52
|
+
"@rollup/plugin-node-resolve": "^11.2.1",
|
|
53
|
+
"@types/react": "^17.0.2",
|
|
54
|
+
"@types/react-dom": "^17.0.2",
|
|
55
|
+
"rimraf": "^5.0.5",
|
|
56
|
+
"rollup": "^2.56.3",
|
|
57
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
58
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
59
|
+
"rollup-plugin-typescript2": "^0.29.0",
|
|
60
|
+
"typescript": "^5.3.3",
|
|
61
|
+
"tslib": "^2.6.2"
|
|
62
|
+
}
|
|
63
|
+
}
|