本页尚未翻译,因此以英文显示。 参与翻译

Enrich top merchants concurrently

FxDart wins async

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

Apple M1 Max, 32 GB RAM · Dart 3.12.2 (AOT-compiled) · 2026-08-24

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

Native Dart 214 µs
FxDart 237 µs

Peak memory Tie

Native Dart 16.6 MB
FxDart 16.6 MB

N = 10,000

Time Native wins

Native Dart 17.7 ms
FxDart 21.5 ms

Peak memory Tie

Native Dart 49.3 MB
FxDart 50.4 MB

N = 100,000

Time Native wins

Native Dart 195.0 ms
FxDart 233.0 ms

Peak memory Tie

Native Dart 81.4 MB
FxDart 84.0 MB

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.