This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
feat(resval v1.0.0-beta) - rendering optimization using cache #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
React Resval v1.0.0-beta - Rendering optimization
What's going on here?
Over time, an application will have a bloated size, as well as the use of Resval. The pull request shows that there are two things that have been optimized. The first is when the component re-renders and the second is when the viewport changes. The temporary idea being implemented is to use the cache concept.
Re-render Component Optimization
We know that when a component uses a certain hook, the computation of that hook will be performed every time the component re-renders. Take a look at the code below:
In the initial render, we know that
useVx
already has a value. This value will always be stable when the component it contains re-renders. A simple idea to avoid unnecessary redundant computations, Resval uses a cache withnew Map()
. The result of the implementation is as shown below with the help of React Profiler.In the initial render, of course, it will do the Resval computation for the first time (in this benchmark it uses
1000_000 ops
). However, in the next rendering of the component, the computation will no longer be performed, because it is already stored in the cache.Viewport Change Optimization
Optimization for components that perform rendering resolved. Hmm.. ok, but what about the timestamps that occur when the viewport changes multiple times!
Yep! Resval uses the same concepts as re-rendering components to optimize for this case. The idea is very simple, every time the viewport changes, the result of the computation will be put into the cache, not during the initial render. In short, the first change of viewport will certainly do the computation, but when the viewport returns to a certain size, the value of
useVx
is just called from the cache. See the benchmark results as shown below:It will only do large computations in the initial render stage and only viewport changes. Otherwise it will fetch the
useVx
value from the cache.How do we benchmark?
The benchmark that was carried out in this test was to create a
1000_000
property for objects inuseVx
,createResponsiveValues
must be defined outside the component or in another module, don't let it be defined inside the component because it will always re-compute every time the component re-renders.