You can read the related blog post over here: https://community.esri.com/t5/arcgis-online-blog/improving-expression-performance-a-custom-function/ba-p/1288785
In short, complex expressions involving multiple layers and loops can quickly lead to extremely large numbers of queries being sent to the respective services, leading to extra unnecessary work for the servers, and additional processing time for the end user.
I wrote a custom function (see linked post above) to build a 100% in-memory FeatureSet, and I have found it an improvement in nearly all cases. But it was only after years of putting up with poorly performing expressions that drove me to investigate what is really a simple solution.
It would be great if this kind of option were built in. I don't think every expression would benefit from this, so perhaps make it an optional parameter, as @JohannesLindner suggests in his response to my post. If it defaults to the current method, everybody's expressions keep working, no problem. But for those of us with more complex expressions, we could specify that we want our FeatureSet "memorized" and reap the performance benefits.
var fs = FeatureSetByPortalItem(portalObject, itemId, layerId?, fields?, includeGeometry?, memorize?)