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

Line items to invoice summary

FxDart wins

Requirement

An order's line items each have a product, category, quantity, and unit price. Print the invoice summary: one line per category with its total (quantity × unit price, summed), largest category first, then a grand total. The data is in the code below; both versions must print the lines shown under Expected output.

Expected output
Invoice by category:
Electronics: $124.87
Office: $75.80
Pantry: $29.50
Total: $230.17

Side by side

Native Dart

FxDart

Why they differ

The same amount, qty * unitPrice, is summed twice — per category and overall — and the two versions treat that differently. Native Dart spells it as two unrelated idioms: a mutating for loop into a map for the categories, then a fold with an explicit seed for the grand total. FxDart says "sum of a field" the same way both times — sumBy — once per groupBy group and once over all items, with sortBy ranking the rows. When the invoice grows a discount rule, one vocabulary changes in one place per pipeline; the native version changes a loop body and a fold.

Benchmark

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

N = 100

Time Tie

Native Dart 4.5 µs
FxDart 8.2 µs

Peak memory Tie

Native Dart 16.5 MB
FxDart 16.5 MB

N = 10,000

Time Tie

Native Dart 243 µs
FxDart 111 µs

Peak memory FxDart wins

Native Dart 22.4 MB
FxDart 15.8 MB

N = 1,000,000

Time FxDart wins

Native Dart 25.3 ms
FxDart 13.6 ms

Peak memory Tie

Native Dart 92.3 MB
FxDart 88.4 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.