You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -96,6 +96,41 @@ You can also use magic methods with for cleaner syntax:
96
96
97
97
For a full list of available conditional methods, see the [Available Conditional Methods](reference/conditional-methods.md) reference.
98
98
99
+
### Performance Considerations
100
+
101
+
Please be aware that heavily relying on conditional methods can lead to performance implications depending on how they are utilized within your resources.
102
+
103
+
During the execution of `toArray`, all _directly accessed_ model attributes, model accessors and function calls **will be computed, no matter the current state of the given Stateful Resource**.
104
+
105
+
Here's an example of a Stateful Resource that may compute more than is needed when the state is `Minimal`:
106
+
107
+
```php
108
+
public function toArray(): array
109
+
{
110
+
return [
111
+
'email' => $this->email,
112
+
'phone' => $this->phone,
113
+
'address' => $this->unlessStateMinimal($this->address), // ⚠️ `$this->address` will always be computed although its value may not be used
114
+
];
115
+
}
116
+
```
117
+
118
+
The impact for smaller resources is _minimal_, as the overhead of computing unused attributes is negligible. However, for large resources with many attributes, this may lead to performance issues.
119
+
120
+
But there is a way to circumvent this limitation by wrapping the value in a closure:
0 commit comments