Caching the transform was one for the first Unity optimizations I ever learned. It was drilled into me like scripture. But things change all the time, and Unity is the fastest evolving software platform I have ever used. Kudos to you Unity! Now I started using Unity back at the end of the 2.x cycle and we have come along way. IL2CPP is freaking amazing. The more profiling I have been doing, the more blown away I am by it. You will clearly see why by the results.
Enough blather, what we are going to be doing here is evaluating our assumption that caching the transform object is faster than referencing it from script or a game object. We are going to be taking our readings from device to be as accurate as possible. In this case, a Samsung Galaxy S8 (Android). I will be providing a link to the evaluation project so please report back with numbers from other platforms if you get different results. Test results from the editor are unreliable and don’t allow you to make use of IL2CPP optimizations.
I wanted to check both performance and memory allocation here so I had to do some shenanigans. The most accurate way to get memory allocation readings that I know of is the Unity Profiler. To use that effectively I needed to separate the tests so that I could fire them manually after I had connected the profiler. So I made UI but the Unity canvas and event system creates its own garbage, especially…