Top 5 merchants by total spend
Requirement
Given a month of ledger transactions — each with a date, merchant, and amount — find the five merchants you spent the most at: group by merchant, total each group, sort the totals descending, and print the top five. The data is in the code below; both versions must print the lines shown under Expected output.
Expected output
Green Grocer: $81.95 Electric Co: $60.34 Noodle Bar: $40.00 Cafe Aroma: $36.50 Book Nook: $27.99
Side by side
Native Dart
FxDart
Why they differ
Core Dart has no grouping at all, so the native version pulls in
package:collection — and grouping there ends the
chain: groupListsBy hands back a Map, so ranking
it means naming an intermediate variable, re-entering through
.entries, and reading each group as an untyped
kv.key / kv.value pair. Sorting adds two more
workarounds: an explicit <num> type argument (inference
fails because double is Comparable<num>,
not Comparable<double>) and a negated key,
since sortedBy only sorts ascending.
In FxDart the four steps are four links of one chain, top to bottom in the
order the requirement states them. groupedBy stays inside the
pipeline — it yields (key:, items:) groups instead of a map,
so nothing has to be unpacked and re-wrapped — and
sortByDesc says “descending” in its name instead
of encoding it as a minus sign. No intermediate variable, no type-argument
ceremony, no sign trick: the code says group, rank, take five.
Benchmark
N = 100
Time Tie
Peak memory Tie
N = 10,000
Time Tie
Peak memory Tie
N = 1,000,000
Time FxDart wins
Peak memory FxDart wins
Bars are medians of repeated timed iterations in fresh processes per side (small N is batched for timer resolution). Sides within 5% of each other — or within 0.6 ms, a difference no person can perceive — count as a tie; close relative races are re-measured up to 5 runs. In an app, anything under a few milliseconds is invisible to the user regardless of which bar is shorter. Memory is peak process RSS. The Dart VM and the dataset are identical on both sides, so the difference between the two bars is what the pipeline itself holds onto.