In my day job, one of my responsibilities is to oversee the technical evolution of our productÂ and to optimize its performance. I’ve even written a few guidelines that detail numerous recommendations for maximizing performance of Android apps in general.
One of the things I have always recommended is to reduce the complexity ofÂ View hierarchies, and try not to overcrowd or have too many nesting levels of your Views, since this can supposedly have a negative impact on performance.Â However, I made these statements based on common sentiment on the web, and based on the AndroidÂ documentation, instead of on actual hard evidence.Â So IÂ looked into it from an interesting perspective:Â I dug into View hierarchiesÂ as they are used by other major apps, and compared them with our own usage.Â This isn’t a totally “scientific” analysis, and it only looks at a single facet of proper View usage. Nevertheless the findings are rather surprising, and are actually challenging my insistence on View minimalism.
I looked at the Twitter, Facebook, and Slack apps, and compared each of their “feed” screens to the feed screen of our own Wikipedia app. (The reason I chose these apps is that the “performance” of their feeds is nearly perfectly smooth, especially considering that some of their content includes auto-playing videos and animations.)Â I used the superbly useful and little-known UI Automator Viewer tool, which is bundled with the Android SDK, to explore these view hierarchies.
For reference, the deepest nesting that I found in the feed of the Wikipedia app is seven (7) levels deep:
But get ready:Â The deepest nesting in the Slack app is…Â eighteenÂ (18) levels deep. And yet it performs perfectly smoothly:
The deepest nesting in the Facebook app isÂ twentyÂ (20) levels deep. And yet it works just fine:
The deepest nesting in the Twitter app isÂ twenty threeÂ (23) levels deep, which includes eight (8) nesting levels for each item in their ListView (it’s not even a RecyclerView!). And yet I’m able to scroll the Twitter feed infinitely without a single hiccup.
Therefore I’m compelled to reevaluate the importance we should be placing onÂ optimizingÂ View hierarchies, at least from the perspective of “nesting.”Â Indeed, this seems to be yet another case for balancing reasonable performance guidelines with more immediate product goals, or put more simply, avoidingÂ prematureÂ optimization.