r/FlutterDev • u/RandalSchwartz • Oct 02 '26
Discussion That innocent-looking compareTo callback might be freezing your 60 FPS timeline
A few days ago, a Flutter developer wrote a profiling post about sorting 10,000 timeline events. Under a section they called "The Original Sin", they pointed out the culprit:
items.sort((a, b) => DateTime.parse(a.start).compareTo(DateTime.parse(b.start)));
In an O(N log N) sort, that callback runs over 215,000 times for 10,000 items. On a mobile isolate, parsing 215,000 ISO strings takes 180ms+ and drops a dozen frames.
To fix it, they mapped the list to a temporary helper object with pre-parsed dates, sorted by the cached key, and unwrapped it.
That Map -> Sort -> Map pattern was actually what I posted to Usenet back in 1994 (what became known as the Schwartzian Transform). But what surprised me running benchmarks in modern Dart: package:collection's sortedBy does not cache keys either—it still ran 127,000 redundant key evaluations because mergeSortBy re-evaluates keys during merges.
With Dart 3 records, you don't even need throwaway helper classes anymore—a boxed record one-liner or simple extension method runs in 14ms (13x faster).
Have you ever caught a subtle frame stutter hiding inside a sort or compare callback?
11
u/driftwood_studio Oct 02 '26
I get the point, and don't disagree... but... I mean anyone who's dealing with an application that's sorting 10,000 of something who hasn't bothered to do a little extremely basic profiling and educate themselves even minimally about the very well documented principles of sorting algorithms... and who is writing code that's doing that sorting in a way that blocks the main rendering thread...
I'm not sure this kind of information is going to help them. They're not the programmers who even really understand there's something they don't know here.
You have to not really know what you're doing to some degree to even get into this kind of situation.
Which... welcome to the imminent future of vibe coded software by non-developers.
7
u/samrawlins Oct 02 '26
Love the post, but the title is misleading.
That innocent-looking DateTime.parse call which is repeatedly called thousands of times on each value, might be freezing your 60 FPS timeline.
The compareTo calls are not reduced and are not the problem.
This reminds me of another foot gun: iterating over a lazy Iterable repeatedly:
i is an Iterable, not a List. Each time you iterate over it, you repeat those expensive calls. I've considered a lint rule for this but never been able to design one that doesn't report a lot of false positives.
Similarly, doing expensive calculations in a for-loop that don't depend on the loop variable:
It would be easier to write this lint rule, but maybe just not very high value.
14
u/Honey-Entire Oct 03 '26
Thanks for all those words, Claude. I’m impressed you’ve discovered the novel idea of avoiding iterating over lists multiple times.
4
u/virtualmnemonic Oct 03 '26
You shouldn't need to parse it to sort properly to begin with.
1
u/Lanky-Foundation8727 Oct 03 '26
I don't think caching keys is the better choice universally. Maybe if calculating the key is slow, like in this case
but also it might be it's just a field getter
in that case, allocating a new list, allocating a tuple for every single element, sorting, then mapping that back or alternatively, allocating a hashmap as a cache for the keys might be slower. not sure how well dart optimizes this but in theory it can be zero-cost that way.