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

Spending inside a date window

Toss-up

Requirement

A ledger export is already sorted by date. Total the spending between 2026-07-08 and 2026-07-21 inclusive — without scanning the whole list: skip entries before the window, take entries while still inside it, and sum what remains. The data is in the code below; both versions must print the line shown under Expected output.

Expected output
Spent 2026-07-08 to 2026-07-21: $104.04

Side by side

Native Dart

FxDart

Why they differ

Barely. Dart ships skipWhile and takeWhile on every Iterable, and both are lazy — the plain-Dart version exploits the sort order exactly the way the FxDart one does, and reads just as well. The only real difference is the last step: sumBy names the intent, where fold spells out the seed and the combine. That is a one-word win, not a structural one — call it a tie. If your codebase already chains with fx, use it here for consistency; if not, plain Dart is fine.

Benchmark

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

N = 100

Time Tie

Native Dart 2.2 µs
FxDart 1.6 µs

Peak memory FxDart wins

Native Dart 16.5 MB
FxDart 15.3 MB

N = 10,000

Time Tie

Native Dart 176 µs
FxDart 110 µs

Peak memory FxDart wins

Native Dart 20.8 MB
FxDart 15.6 MB

N = 1,000,000

Time FxDart wins

Native Dart 14.9 ms
FxDart 11.4 ms

Peak memory Tie

Native Dart 124.3 MB
FxDart 120.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.