Hello,
List.lastWhereOrNull(...) (from IterableExtensions) is inefficient for lists compared to calling List.reversed.firstWhereOrNull(...).
Consider this example:
final myList = List.generate(1_000_000, (i) => i);
final slowImpl = myList.lastWhereOrNull((i) => i >= 900_000);
final fastImpl = myList.reversed.firstWhereOrNull((i) => i >= 900_000);
In this case, slowImpl is computed by iterating through 1,000,000 elements. On the other hand, fastImpl completes after iterating through only 1,000 elements.
Since iterable does not have a reversed property (and since we would not know if it is efficient if there was one), I propose creating a ListExtensions extension (an idea also floated here) that can take advantage of List.reversed being an O(1) operation.
Hello,
List.lastWhereOrNull(...)(fromIterableExtensions) is inefficient for lists compared to callingList.reversed.firstWhereOrNull(...).Consider this example:
In this case,
slowImplis computed by iterating through 1,000,000 elements. On the other hand,fastImplcompletes after iterating through only 1,000 elements.Since iterable does not have a
reversedproperty (and since we would not know if it is efficient if there was one), I propose creating aListExtensionsextension (an idea also floated here) that can take advantage ofList.reversedbeing an O(1) operation.