I have a GridCell component, which will be instantiated thousands of times.
The component requires access to its parent components (GridRow and Grid). Value of those parameters should never change.
How does following alternatives compare in terms of performace?
Using CascadingParametes
GridCell:
[CascadingParameter(Name = "DataGrid")] DataGrid DataGrid { get; set; } [CascadingParameter(Name = "Row")] DataGridRow Row { get; set; }usage in GridRow:
<Cell />Parameters
GridCell:
[Parameter] DataGrid DataGrid { get; set; } [Parameter] DataGridRow Row { get; set; }usage in GridRow:
<Cell DataGrid="@Grid" Row="@this" >
1 Answer
It depends on IsFixed attribute:
<CascadingValue Value="this" IsFixed="true">
<SomeOtherComponents>
</CascadingValue>
- If the IsFixed value is false (the default), then every recipient of the cascaded value sets up a subscription to receive change notifications. In this case, each [CascadingParameter] is substantially more expensive than a regular [Parameter] due to the subscription tracking.
- If the IsFixed value is true (for example,
<CascadingValue Value="@someValue" IsFixed="true">), then receipients receive the initial value but do not set up any subscription to receive updates. In this case, each [CascadingParameter] is lightweight and no more expensive than a regular [Parameter].
Learn about this topic and other performance topics at Ensure cascading parameters are fixed MS docs.