roslynator
roslynator copied to clipboard
Suggested fix for RCS1077 runs slower
Great work guys!
Product and Version Used: 3.2.2
Running on ASP.NET 4.8, RCS1077 triggers for this snippet:
_items.Where(x => x.Id == findId).SingleOrDefault();
Suggested fix is as follows but runs 250% slower (20,000 iterations):
_items.SingleOrDefault(x => x.Id == findId);
Thanks for you appreciation and sorry for the later answer.
I think thank it's possible that the code with the fix can run slower if the collection is List or array but generally this analyzer simplifies code and improves readability.
If it's on the hot path then I think the best solution is to suppress the warning.
I would classify this as an issue with .NET Framework. Though as .NET Framework isn't actively developed I don't think it will be resolved there. Someone (if I find time I could, but not soon) should validate if the same issue persists in .NET 6.
It does. Simple test in LinqPad shows SingleOrDefault is 2x slower. If you have a List<T> then Find(x => x.Id == findId) is 6x faster than SingleOrDefault(x => x.Id == findId)
Yeah. As long as it's an issue in .NET, it should not be suggested.
Several fixes are part of RCS1077
, maybe each should have an eslint config to turn off/on?