Anomalies with surrounding context
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
N = 100
Time Tie
Peak memory Tie
N = 10,000
Time Tie
Peak memory Tie
N = 1,000,000
Time FxDart 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.