1.5.0: Optimization on state derivations and side effects #290
Replies: 4 comments 2 replies
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks @pearmini a lot for the contribution! |
Beta Was this translation helpful? Give feedback.
-
|
Below is an example of using this feature to create a custom const {button} = van.tags
class MyButton extends HTMLButtonElement {
connectedCallback() {
this.addEventListener("click", () => alert("MyButton clicked!"))
}
}
customElements.define("my-button", MyButton, {extends: "button"})
const CustomButton = () => button({is: "my-button"}, "Click me")
van.add(document.body, CustomButton())With this feature, the bundle sizes increase slightly. Gzipped bundle increases to Thanks @maburdi94 a lot for the contribution! |
Beta Was this translation helpful? Give feedback.
-
|
There is no implementation change in Thanks @carlosmintfan for reporting the issue! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi fellow VanJSers,
I'm happy to announce the release of VanJS
1.5.0. 🎉🎉🎉✨ What's in the release?
1. Optimization on state derivations
In this release, we made major optimization on how state derivations are being executed. To illustrate the optimization, let's consider an example below:
Before
1.5.0, if we have:swill be derived twice, one triggered by++a.val, and the other triggered by++b.val.1.5.0changed the execution of state derivations from synchronous to asynchronous, so that only one derivation forswill be executed after both stateaandbare updated.Another benefit of this optimization is that, if we have:
The derivation of
swill be skipped because after++a.valand--a.valthevalofadoesn't change at all.For state-derived DOM properties and DOM child nodes, we already have this kind of optimization since
0.11.9(VanJS's first public release).1.5.0release expands the optimization to state derivations. Side effects defined byvan.derivewill also be benefited from this optimization.Note that in one single derivation cycle, state derivations and side effects are always executed before DOM updates, to ensure DOM re-renderings always have the top-priority to be minimized. For instance, for the component:
When the
valofachanges, the derivation ofswill be executed 4 times (triggered by the updates of statea,b,c,d, respectively). Buts^2, thes-derived Text node will be re-rendered only once.The optimization in
1.5.0is very crucial forVanX-based apps. For instance, let's say we have the following piece of code to save the reactive objectitemsintolocalStoragewhenever it changes (as in this example):Prior to
1.5.0,localStorage.setItemmight be called multiple times for updates toitems, especially if we updateitemswithvanX.replace(items, ...), leading to significant waste of resources. With the optimization in1.5.0,localStorage.setItemwill be called only once for each update and derivation cycle.2.
rawValproperty forStateobjectsIn
1.5.0, we added a new readonlyrawValproperty forStateobjects - for getting the current value of theStateobject (peeking) without registering the state as a dependency of the binding function for the derived state, side effect or DOM node. For instance, the derived state:will be updated when
bchanges, but won't be updated whenachanges. Being able to "peek" the value of a state without registering the dependency can be useful for some use cases, such as: #282.Similarly, we also released VanX to
0.3.0to introduce a new functionvanX.rawfor getting the field value of a reactive object without registering the dependency. e.g.:will make
data.supdated whendata.bchanges, butdata.swon't be updated whendata.achanges.With the optimization, the bundle sizes increase slightly. Gzipped bundle increases to
1055 bytes(1.0kB) from1012 bytes(1.0kB) (43 bytesincrease), while minified bundle increases to2035 bytes(2.0kB) from1896 bytes(1.9kB) (139 bytesincrease) - still being the smallest reactive UI framework in the world.❤️ Hope you can enjoy!
Beta Was this translation helpful? Give feedback.
All reactions