Enrich top merchants concurrently
Requirement
From July's per-merchant spend totals, take the top three merchants and enrich each with its category from a (simulated) merchant-directory API — but the API rate-limits, so never more than two lookups in flight at once. Results print in spend order after all lookups finish, and the fake lookup counts overlapping requests so both versions can prove the limit held. The data is in the code below; both versions must print the lines shown under Expected output.
Expected output
Green Grocer — $118.65 (Groceries) Electric Co — $60.34 (Utilities) Noodle Bar — $54.70 (Dining) max lookups in flight: 2
Side by side
Native Dart
FxDart
Why they differ
The task changes character halfway through — synchronous ranking, then
rate-limited I/O — and only one version's code changes character with
it. In FxDart the seam is a single chain step: sortBy +
take pick the merchants, toAsync crosses
into async, and map + concurrent(2) run the
lookups two at a time, in order. Native Dart has no primitive for
"at most two in flight": Future.wait fires everything at
once, so the bounded half becomes a hand-rolled worker pool — shared
cursor, pre-sized result slots, worker futures — that dwarfs the
two-line ranking it serves. Changing the limit, or dropping it, is
one number in the chain versus that whole scaffold.
Benchmark
Async case: the headline scale is N = 100,000, not 1,000,000. Every element costs an event-loop turn on both sides, so a million real awaits would measure Dart's event loop for minutes — not the pipeline. Delays are zero-length and the example's concurrency limit is kept; what the bars compare is the pipeline machinery.
N = 100
Time Tie
Peak memory Tie
N = 10,000
Time Native wins
Peak memory Tie
N = 100,000
Time Native wins
Peak memory Tie
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.