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

Anomalies with surrounding context

FxDart wins

Requirement

A temperature sensor logged ten readings (data in the code). Print every reading above 80.0 C — marked with ! — together with the reading directly before and after it, the way grep -C1 shows context lines. Where context windows overlap, each reading appears once. Finish with the peak reading. Both versions must print the block under Expected output.

Expected output
Readings above 80.0 C, with context
  09:05  64.8 C
! 09:10  91.2 C
  09:15  66.0 C
  09:20  67.4 C
! 09:25  84.9 C
! 09:30  88.3 C
  09:35  70.2 C
Peak: 91.2 C at 09:10

Side by side

Native Dart

FxDart

Why they differ

"Each hit expands to a window, then overlapping windows merge" is a flatten-plus-dedupe problem, and FxDart spells it exactly that way: zipWithIndex keeps positions, filter finds the anomalies, flatMap expands each into [i-1, i, i+1], and uniq merges the overlaps — one uninterrupted expression from readings to printed lines. Native Dart has no flatMap-into-uniq idiom for this, so the natural version builds a Set<int> of indices in nested for loops, sorts it, and formats in a second loop — the same algorithm, but split into three mutable phases.

Benchmark

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

N = 100

Time Tie

Native Dart 1.6 µs
FxDart 1.5 µs

Peak memory Tie

Native Dart 16.5 MB
FxDart 16.4 MB

N = 10,000

Time Tie

Native Dart 113 µs
FxDart 93 µs

Peak memory Tie

Native Dart 18.3 MB
FxDart 18.7 MB

N = 1,000,000

Time FxDart wins

Native Dart 11.9 ms
FxDart 10.4 ms

Peak memory Tie

Native Dart 132.2 MB
FxDart 131.7 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.