When a Flutter timeline sorted 10,000 activities, parsing each ISO-8601 timestamp inside the sort comparator caused enough repeated work to contribute to frame drops. Randal L. Schwartz’s fix was to parse each timestamp once, sort cached key-and-item pairs, then recover the activities: the Schwartzian Transform, often summed up as “Map → Sort → Map.”
Why sorting 10,000 timeline items caused jank
A sort comparator is called repeatedly as the algorithm compares items. Its call count can therefore be much larger than the number of items being sorted. In the Flutter incident Schwartz describes, the comparator parsed a timestamp string with DateTime.parse(activity.start) each time it needed to compare two activities. The same strings were parsed over and over.
As an Amazon Associate I earn from qualifying purchases.
The issue was not that parsing one timestamp is necessarily expensive. It was that the parsing work sat inside an operation that ran many times. For a large timeline, that repeated work contributed to frame drops; caching each parsed date before sorting removed the repeated parsing that caused the reported freeze.
What the Schwartzian Transform does
The transform separates key calculation from comparison. It decorates each item with its sort key, sorts those temporary pairs using the already-computed keys, and then removes the keys to return the original items.
#1 Best Overall
- Map: calculate the derived key once for each item and pair it with that item.
- Sort: compare the cached keys, not the original data that requires expensive processing.
- Map: extract the original items in their new order.
For the timeline, the key is a parsed DateTime. Once each date has been parsed, comparisons use DateTime.compareTo rather than parsing timestamp text again.
Implementing cached-key sorting with Dart 3 records
Dart records offer a compact way to hold a temporary key and its corresponding item without defining a separate helper class:
Rank #2
final sorted = [
for (final item in widget.activity)
(key: DateTime.parse(item.start), item: item),
]..sort((a, b) => a.key.compareTo(b.key));
final result = [for (final entry in sorted) entry.item];
Records are anonymous, immutable aggregate values with named or positional fields. Dart documents them as fixed-size, heterogeneous, and typed; records require language version 3.0 or later. Here, each record keeps a parsed date and its activity together throughout the sort.
A reusable extension can apply the same pattern to any comparable derived key:
extension SchwartzianSortExtension<T> on Iterable<T> {
List<T> sortedByExpensive<K extends Comparable<K>>(
K Function(T item) keyOf,
) {
final boxed = [
for (final item in this) (key: keyOf(item), item: item),
]..sort((a, b) => a.key.compareTo(b.key));
return [for (final entry in boxed) entry.item];
}
}
This version evaluates keyOf once per input item. It returns a new list and uses additional temporary records and a decorated list; it does not mutate the original iterable.
What the reported benchmark shows—and what it does not
Schwartz reports the following results for sorting 10,000 ISO-8601 timestamp items. These are his 2026, author-reported measurements, not a universal Dart or Flutter benchmark:
Rank #4
| Approach | DateTime.parse evaluations |
Reported time |
|---|---|---|
Naive List.sort with parsing in the comparator |
215,462 | 186 ms |
package:collection sortedBy() |
127,590 | 107 ms |
| Cached-key Schwartzian implementation | 10,000 | 14 ms |
In that test, Schwartz describes the cached-key implementation as 13.3 times faster than the naive baseline and says it fit within a 60 FPS animation tick. A 60 FPS frame budget is about 16.7 ms, but a sort taking 14 ms in one test does not establish that an entire Flutter frame will meet its budget on other devices or under other workloads. Build mode, hardware, input data, allocation, and surrounding UI work all affect the result.
Schwartz also reports that the inspected sortedBy implementation called its key function during merge or insertion operations rather than caching one result per item. The public API documentation establishes that sortedBy and sortBy are available; it does not, by itself, establish the internal call behavior for every package version. Treat the evaluation count as specific to Schwartz’s inspected implementation and benchmark.
Best Value
Keep equal-key ordering deterministic
Dart’s List.sort does not guarantee a stable sort: items that compare equal are not guaranteed to remain in their original relative order. That can make a timeline appear to reshuffle activities with identical timestamps.
If that matters, decorate each item with its original index as a secondary key. The index preserves input order among equal dates:
final sorted = [
for (var i = 0; i < widget.activity.length; i++)
(key: DateTime.parse(widget.activity[i].start), index: i,
item: widget.activity[i]),
]..sort((a, b) {
final byDate = a.key.compareTo(b.key);
return byDate != 0 ? byDate : a.index.compareTo(b.index);
});
final result = [for (final entry in sorted) entry.item];
The explicit tie-breaker makes the intended order part of the comparator instead of relying on the sorting algorithm to preserve it.
When cached-key sorting is worth the extra allocation
The transform is most useful when both conditions apply: the collection is large enough for repeated key work to matter, and deriving the key is meaningfully more expensive than comparing it. Parsing dates is one example; Schwartz also identifies regular-expression evaluation, decoding, metadata lookup, and hashing as possible expensive key calculations.
- Consider caching: parsing timestamp strings or computing another non-trivial key for many items, especially when the sort runs in an animation-sensitive path.
- Prefer a simpler sort: sorting on an existing integer,
DateTime, or other cheap primitive field. In that case, the temporary list and records may add allocation and copying without saving substantial CPU work. - Consider storing the key: if the derived value is reused elsewhere or can be maintained safely on the model, persistent caching may avoid recomputation across multiple operations. That choice adds model state and requires keeping the cached value in sync with its source data.
- Measure in context: compare alternatives in the build and on the devices that matter to the app, and observe both elapsed time and allocation behavior.
The idea has a history beyond Dart: the decorate-sort-undecorate pattern is associated with Schwartz’s 1994 Perl-era work. In 2026, he recognized the same opportunity in a Flutter performance discussion and commented, “Almost looks like you could have used a Schwartzian Transform. :)”
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




