-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Performance | ||
|
||
Collection of things to avoid and improve to have a better performance. | ||
|
||
### 1. Using use_effect to synchronize state | ||
The `use_effect` hook is sometimes missused as a synchronization between states | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut state = use_signal(|| 1); | ||
let mut double_state = use_signal(|| 1); | ||
|
||
use_effect(move || { | ||
// Update double_state whenever `state` changes | ||
double_state.set(state() * 2) | ||
}); | ||
|
||
rsx!( | ||
label { | ||
onclick: move |_| state += 1, | ||
"{state} * 2 = {double_state}" | ||
} | ||
) | ||
} | ||
``` | ||
|
||
This is bad because we are storing a derived value (double_state) in a reactive wrapper (signala). | ||
The flow would have been: | ||
``` | ||
(initial) -> state: 0 , double_state: 0 | ||
(state gets updated) -> state: 1 , double_state: 0 | ||
(effect runs and updates double_state) -> state: 1 , double_state: 1 | ||
``` | ||
|
||
|
||
#### Manual signal derivation | ||
|
||
We can simply create a temporary variable in which to store the derived value from the signal. | ||
Because we are reading `double_state`, whenever `state` changes this component function will reerun, so `double_state` will always be up to date. | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut state = use_signal(|| 1); | ||
let double_state = state() * 2; | ||
|
||
rsx!( | ||
label { | ||
onclick: move |_| state += 1, | ||
"{state} * 2 = {double_state}" | ||
} | ||
) | ||
} | ||
``` | ||
|
||
Now, the flow would be: | ||
``` | ||
(initial) -> state: 0 , double_state: 0 | ||
(state gets updated and double_state derived) -> state: 1 , double_state: 1 | ||
``` | ||
|
||
### Reactive signal derivation | ||
|
||
We can also use `use_memo` to memoize derived values. This is very useful for values that are expensive to compute (which isn't the case with simple numeric operation) | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut state = use_signal(|| 1); | ||
let double_state = use_memo(move || state() * 2); | ||
|
||
rsx!( | ||
label { | ||
onclick: move |_| state += 1, | ||
"{state} * 2 = {double_state}" | ||
} | ||
) | ||
} | ||
``` | ||
|
||
The flow would be: | ||
``` | ||
(initial) -> state: 0 , double_state: 0 | ||
(state gets updated and double_state memo run synchronously) -> state: 1 , double_state: 1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Memoization | ||
|
||
You can memoize values by using the `use_memo` hook. This can be useful to have reactive derived across components or to cache expensive value to compute. | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut state = use_signal(|| 1); | ||
// `use_memo` returns a `ReadOnlySignal`, as the name says it is a Signal | ||
// that you can read and subscribe to but you cannot mutate | ||
// as its value can only be changed when the memo runs | ||
let double_state = use_memo(move || { | ||
// Just like `use_effect`, whenever a signal that is read in here is changed, the memo will rerun. | ||
state() * 2 | ||
}); | ||
|
||
rsx!( | ||
label { | ||
onclick: move |_| state += 1, | ||
"{double_state}" | ||
} | ||
) | ||
} | ||
``` |