Posted on 08/11/2017 09:58:13
Hi Claus,
Alright, I guess I have to use a bit of slightly simplified math to show the issue. I know this stuff is a bit difficult, but I don't see how to explain it without a bit of notation.
var servicePages = pageService.GetPagesByParentID(123)
The above operation looks in a dictionary to find the parent with id 123: O(1).
Then it looks at where this parent is in the tree: O(1).
Then it returns all children at this position in the tree: O(#children) = O(n) <--- Can be improved to (1)
Then it sorts by id: O(n) <--- Can be improved to O(1)
Total: O(2n) <--- Can be improved to O(1)
.Where(x => x.Published)
The method above loops through all elements: O(n) <--- Cannot be improved!
Each element accesses Published (a simple boolean property): O(1)
Total: O(n)
This means, that roughly speaking, you spend between 50% and n orders more time (if we spend time to improve performance of this method in the future) to filter out all the pages that are not published, then you do just returning all the pages.
If we end up not supporting the typical use-case, we ofc. need to change the datastructure to support it, such that what you typically do is fast.
Foreach, linq and parameters are all going to do exactly the same, so there is no significantly difference (actual runtime by vary alot, but this will change depending upon the version of the JIT compiler).
BR
Martin