In a asp.net menu that does loads of recursion with loads of menu items (in order of 100’s) what would be the most performing code i can write to check if the current menu item is the last in the list of items. I came up with this but i would like to improve this, i would mark a code as answer only when they can explain how they their code is optimized and performing.
if (childMenuItems.Parent.ChildItems.IndexOf(childMenuItem) == childMenuItems.Parent.ChildItems.Count -1)
Inputs
childMenuItem is instance of MenuItem which is present in the Menu class
In my opinion, this is optimised because you’re doing a straight array index which is cheap rather than doing
IndexOf.IndexOfis expensive because it starts at the beginning of the list and equality compares each element againstchildMenuItem.In this solution there is only one equality check which is the most expensive part.
UPDATE This solution assumes that there are always elements in the list