Intermediate results (dereferencing, CSE, ..) - when to avoid?

I think there’s some misconception here. The intermediate is still allocated, even if you don’t assign a name to it. The object

fieldnames(typeof(first(v)))

is created and allocated, not naming it doesn’t help, it will just be assigned some internal label instead.

In cases where the intermediate is just some value, that does not need to be heap allocated, the allocations don’t matter either way, and you can create temporary variables to your heart’s content. But if the intermediate is an array, for example, then you get a real, and possibly expensive allocation over and over.

So in your case it’s not a trade-off, it’s just worse in every way to not assign the intermediate.

2 Likes