Comments (1)
Thanks for raising this @ryo-manba. I was just about to request similar as well! Being able to serialize non-primitives so they are cached will be very helpful, especially since fetch
patch is being (temporarily) removed in #28896.
I gave this a little think to see what an API could look like, and I came up with the following:
Introduce a hash function that receives all function arguments and returns array of argument hashes.
function cache(fn, argsHashFn?){}
For example:
const x = cache((vec) => sum(vec), (vec) => [vec.join(',')])
// Example
x([1, 2]) // Cache miss
x([1, 2]) // Cache hit
x([1, 3]) // Cache miss
Interesting cases
- Explicitly hashing some arguments but not others by passing empty element or undefined. In this case, default behavior of hashing by the
arg
itself will apply.
const x = cache((s, vec) => scale(s, vec), (_s, vec) => [,JSON.stringify(vec)])
- Specifying constant hashes to cache all invocations to the same result
const x = cache((s, vec) => scale(s, vec), () => [true, true])
Thoughts
This may introduce some overhead to the function invocation, but if building the hash array is cheaper than invoking the function, then caching in this way makes sense. Otherwise a different approach would be better suited.
Extra
Edit: could not figure this out. It looks like the best solution is from inside
cache
itself.
As I typed this, I thought of a more 'cursed' way of achieving memoized behavior without making changes to existingcache
implementation. Potentially, you could introduce someMap
managed in user-land that could be used to stabilize the references of non-primitive arguments to a function call. Not sure if I know enough to build this out, but I'll give it a go!
from react.
Related Issues (20)
- [Compiler Bug]: Usage of `in` operator is reported as a syntax error HOT 3
- Ok I understand, thanks again
- No good alternative once this happens "Support for defaultProps will be removed from function components in a future major release" HOT 4
- Bug: Nested lazy components cause rerendering HOT 2
- [Compiler Bug]: Invalid drop of memoization for string template literals HOT 1
- [React 19] Allow access of default error callbacks in `onCaughtError` and `onUncaughtError` HOT 2
- [Compiler Bug]: Can't handle Dynamic JavaScript Object Keys HOT 3
- Bug: useEffect runs before DOM changes are painted HOT 5
- [React 19] React 19 runs extra effects when elements are reordered
- [Compiler Bug]: object's get syntax is missing after compilation. HOT 3
- [Compiler Bug]: useMemo returns a result that contains a ref that needs to be declared as a dependency
- [DevTools Bug]: version 5.2.0 icon opien in editor cannot click , color is gray
- Bug: Web worker constructor with URL argument created beforehand fails with MIME type error HOT 2
- [React 19] State dispatch get stuck during an async form action (bug?) HOT 3
- compiler feature request: report error on reassigning a const HOT 1
- [React 19] HOT 1
- [DevTools Bug] Cannot add node "1" because a node with that id is already in the Store. HOT 1
- Bug: Exported ReactDom.version differs from installed one
- Bug: TypeError in Rollup build: parseRequestedNames doesn't handle array input correctly
- Bug: Inaccurate Bundle Type Filtering in Rollup Build
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.