var filteredList = HugeList. Where(Condition);
if(filteredList.Count() > 0)

Just no. Please.

  • 0
    Why not? :S
  • 2
    Count() will evaluate the condition on all elements in the list. Now suppose that list to be extremely huge, and the condition's evaluation to be relatively complex.

    In this particular example it would be way more efficient to write for example:

    var element = HugeList.FirstOrDefault(Condition);
    if(element != null)
  • 0
    I'm just gonna say it: Don't use these stupid functional list manipulation methods full stop if you care about performance
  • 0
    Lets write everything in assembly then...

    Is the same piece of code slower using LINQ than using loops?

    Does that mean that whenever you use LINQ you shouldn't be mindful about performance?
    Absolutely not.
  • 1
    @12bitfloat actually if you're good enough in Linq, performance wouldn't be an issue
  • 1
    @12bitfloat That’s the most retarded comment I’ve read on devrant in a very long time.

    Most of the time these functions implement very good algos. Keep writing your bubble sort manually.

    I can agree, that you need to understand HOW they work in order to avoid triple (or even 10x times) iteration of the initial list. But in 95% of cases even “badly” written statement will be faster than half-baked “custom” solution. And there are special cases when Database is involved.
  • 0
    @NoToJavaScript I didn't talk about sorting. In his example he iterates over a list to find the first element passing the condition. Is it really necessary to slap more layers of complexity on top of a simple solution?
  • 0
    @NoToJavaScript Also funny because you would probably be the first one to speak up against the millions of dependencies of a typical JavaScript app...
  • 0
    @12bitfloat (btw, I have nothing against TypeScript, I just hate writing JavaScript without strong types, "find references", "rename" and other good IDE stuf)

    It depends. I like using “map” and “filter” in JS. And no, I don't like 100000 libs in front end

    But let’s just see what happens if I slightly edit the original code. I only added toArray.

    var filteredList = hugeList.Where(x => x.Number == 1).ToArray();

    Result ? The list was enumerated only once. (note : items are not cloned with ToArray, so memory overhead is very small).

    Edit : FirstOrDefault solution is still better
  • 0
    @dozingncoding that makes so much sense! Thanks 😄
Add Comment