Last post May 22, 2018 03:15 PM by bruce (sqlwork.com)
May 18, 2018 02:41 AM|Ritmo2k|LINK
I was reading a doc page at ms's site that showed several ways to enumerate collections in razor views while considering performance. The details compared foreach for for and had a hint on performance. I have been trying to find that doc (thought I saved
the link) but I failed to.
The suggestions were not as I would have expected and I am really hope to find the link but I have been searching for days without luck:)
Anyone know it by chance?
May 18, 2018 05:15 AM|Edward Z|LINK
It’s sad that I did not see this document before and I fail to find such a document based on your description.
I could only find
this which describe the looping, I would suggest you make a test with then to check the performance.
May 18, 2018 10:57 AM|PatriceSc|LINK
Likely irrelevant compared with other factors and your best bet is alsmot always to test your exact scenario. Are you trying to solve some issue ?
Sometimes the problem is just trying to render far too much data causing the download (and then the browser rendering) of a huge HTML page.
May 18, 2018 11:24 AM|Ritmo2k|LINK
No problem in particular, it was just incredibly informative. It also showed some preferred methods for generating display labels as well.
Each time I try to search for it, all I come up with is the link Edward pointed out.
May 21, 2018 06:34 AM|Edward Z|LINK
I assume the document is dropped off.
If you have any real issue in your production, please feel free to post here.
We could discuss this issue separatly.
May 21, 2018 07:43 AM|PatriceSc|LINK
Try to search for keywords you remember as well as the site name in your favorite search engine. If the link is broken it perhaps moved from msdn to docs or may be it has been just dropped.
May 21, 2018 02:16 PM|bruce (sqlwork.com)|LINK
unfortunately you need to know the details of the collection. unlike the java libraries, the .net collections do not include the O values for iterating or accessing. the killer one is db query objects, which run the database query every time you iterate
them. this is why you usually do a ToList(), so the query is done once.
May 21, 2018 02:57 PM|Ritmo2k|LINK
I have been searching like crazy trying everything I could think of. I only saw the document by coincidence recently while looking for some HTML helper info and I meant to revisit it. One of the interesting parts that I vaguely recall was that it suggested
foreach constructs where not as efficient as for.
I am familiar with the nuances around enumerable queries and multiple trips to a database, this was less obvious.
May 22, 2018 03:15 PM|bruce (sqlwork.com)|LINK
it will depend on the collection design whether for or foreach will be faster. for arrays, for is slightly faster (as foreach creates an iterator that keeps a current index, and still does an index lookup). a hash table iterates by looking the keys collection,
and doing a key lookup. all collections that us array under the covers are often faster with index lookup, but if the index lookup loops to get the item, than it slower.
in any case asp.net core is full of reflection (all binding), and reflection is a lot slower than iteration overhead, so you have lots of other places to look for performance. not using view bag, would do more.