Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

Fetch profiles, two at a time

FxDart wins async

Requirement

Fetch six user profiles from a (simulated) API, but never more than two requests in flight at once — the API rate-limits. Results must come back in the original order. To prove the limit held, the fake fetch counts how many requests overlap and both versions print the maximum observed.

This is the task plain Dart has no primitive for. Future.wait runs everything at once; batching into pairs wastes time waiting for the slower of each pair; doing it right means writing a worker pool by hand — index bookkeeping, a shared cursor, pre-sized result slots. FxDart's .concurrent(2) is that worker pool, as one word: as each request finishes the next one starts, and order is preserved.

Expected output
user#1, user#2, user#3, user#4, user#5, user#6
max requests in flight: 2

Side by side

Native Dart

FxDart

Why they differ

The two versions print the same thing — the difference is what you had to write and what you now have to maintain. The native worker pool is real production boilerplate (and easy to get subtly wrong: off-by-one on the shared cursor, forgetting to pre-size the results list, losing ordering). In the FxDart version the concurrency policy is a single chain step, so changing the limit — or removing it — touches one number instead of the function's whole shape.

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 323 µs
FxDart 360 µs

Peak memory Tie

Native Dart 16.5 MB
FxDart 17.0 MB

N = 10,000

Time Native wins

Native Dart 31.1 ms
FxDart 34.6 ms

Peak memory FxDart wins

Native Dart 26.1 MB
FxDart 22.2 MB

N = 100,000

Time Native wins

Native Dart 305.9 ms
FxDart 339.2 ms

Peak memory Tie

Native Dart 49.3 MB
FxDart 50.1 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.