Dart

Freezed vs Primary Constructors: Build Time and Lines Measured

Table of Contents

This post is for Flutter and Dart developers deciding whether freezed still earns its place now that Dart 3.13 has primary constructors. The freezed README itself now says you “no longer need to use freezed to generate primary constructors”. However, it does not say what freezed costs you in build time, generated code or analyzer load, or what you lose without it. So this post measures freezed vs primary constructors directly. It generates 50, 200 and 500 model classes four ways, times build_runner and dart analyze five times each, and checks what each version actually does at runtime. As a result, you get the cost per model in seconds and lines, plus a clear rule for which style to use where.

The numbers in this post came from this environment:

Measured 2026-11-26
Windows 11 Pro 25H2 (build 26200.9974), AMD Ryzen 5 8600G (6C/12T), 16 GB RAM
Flutter 3.47.9 stable (framework c197ca5199), Dart 3.13.7
freezed 4.0.3, freezed_annotation 3.1.0, json_serializable 6.15.2, json_annotation 4.12.0
build_runner 2.17.1, build 5.0.0, analyzer 14.7.0, source_gen 4.3.1, dart_style 3.1.14
Git for Windows 2.48.1, GNU bash 5.2.37
Pure Dart packages (no Flutter dependency), fresh pub cache
Every timing is the median of 5 runs, rounds interleaved across all packages

This is a Windows desktop. Absolute seconds on a Linux CI runner or an M-series Mac will differ, so read the ratios between rows rather than the raw figures. Also, Flutter 3.50.1 with Dart 3.14.0 is the current stable as of this writing. These runs use the last Dart 3.13 SDK on purpose, because 3.13 is the release that made primary constructors stable.

Freezed vs Primary Constructors: The Short Answer

Freezed is cheap to run but expensive to cold start. Generating code for 500 models takes 2.8 to 6.0 seconds once build_runner is warm. In contrast, every cold build pays about 22 seconds before generating anything, because build_runner 2.14 and later compiles its build script ahead of time. Meanwhile, plain primary constructors cost nothing, but give you no ==, no copyWith and no toString.

500 models, median of 5Plain primary constructorfreezed on primary constructorfreezed factory stylefreezed factory + JSON
Hand-written lines per model10131517
Generated lines per model077269306
Cold build_runner buildnone23.9 s24.9 s27.8 s
Cold build with --force-jitnone12.3 s13.2 s18.4 s
Full regeneration, warmnone2.8 s4.2 s6.0 s
Edit one leaf modelnone1.6 s1.6 s3.1 s
dart analyze1.7 s2.9 s4.3 s5.2 s
Value equality and copyWithNoYesYesYes
Unmodifiable List fieldsNoNoYesYes

In short, the cheapest freezed is freezed on a real primary constructor. It generates 3.5 times fewer lines than the classic factory style, and it analyzes 1.5 times faster at 500 models. However, it quietly drops one freezed guarantee, collection immutability, which the runtime check below shows.

What Dart 3.13 Primary Constructors Give You

A primary constructor declares the fields and the constructor in the class header. The Dart 3.13 announcement describes it as “a Point class with two fields and a constructor in a single line of code”, and the primary constructors documentation covers the full syntax. Here is a realistic model written that way:

// lib/order_plain.dart
// Dart 3.13 primary constructor, no code generation.
class PlainOrder({
  required final String id,
  required final int quantity,
  final double? discount,
  final List<String> tags = const [],
});

The final keyword on each parameter is what makes it a declaring parameter, so each one becomes a field. That is also why Dart 3.13 now rejects final on ordinary parameters, a change the Dart 3.13 breakage count across 100 Flutter apps measured in real code.

However, this class is not a data class. It has identity equality and the default toString. In practice, that means two orders with the same values are not equal, which breaks state comparisons in Riverpod, Bloc and any Set or Map keyed by the model. The runtime check further down prints exactly that.

Writing the Missing Members by Hand

You can add the members freezed would generate. A primary constructor can still have a class body:

// lib/order_handwritten.dart
import 'package:collection/collection.dart';

// Primary constructor plus the members freezed would generate, written by hand.
class HandOrder({
  required final String id,
  required final int quantity,
  final double? discount,
  final List<String> tags = const [],
}) {
  static const _listEq = ListEquality<String>();

  HandOrder copyWith({String? id, int? quantity, double? discount, List<String>? tags}) =>
      HandOrder(
        id: id ?? this.id,
        quantity: quantity ?? this.quantity,
        discount: discount ?? this.discount, // cannot reset to null
        tags: tags ?? this.tags,
      );

  @override
  bool operator ==(Object other) =>
      other is HandOrder &&
      other.id == id &&
      other.quantity == quantity &&
      other.discount == discount &&
      _listEq.equals(other.tags, tags);

  @override
  int get hashCode => Object.hash(id, quantity, discount, _listEq.hash(tags));

  @override
  String toString() =>
      'HandOrder(id: $id, quantity: $quantity, discount: $discount, tags: $tags)';
}

That is 29 non-blank lines for four fields, compared with 7 for the plain version. Moreover, it has a real bug that almost every hand-written copyWith shares. Because null means “not passed”, copyWith(discount: null) cannot clear the discount. Fixing that needs a sentinel object per nullable field, which is exactly the kind of code nobody wants to maintain across 200 models.

The Three Freezed Shapes in This Benchmark

Freezed 4.0.0 added support for primary constructors, according to the freezed changelog. So there are now two ways to write a freezed model, plus the JSON variant most apps use.

First, freezed on a real Dart 3.13 primary constructor. The fields are declared by Dart itself, and freezed only adds a mixin:

// lib/order_freezed_primary.dart
import 'package:freezed_annotation/freezed_annotation.dart';

part 'order_freezed_primary.freezed.dart';

@freezed
class PrimaryOrder({
  required final String id,
  required final int quantity,
  final double? discount,
  final List<String> tags = const [],
}) with _$PrimaryOrder;

Second, the classic factory style that every freezed tutorial before 2026 teaches, including the freezed data classes and unions guide on this site. Here, freezed generates a private implementation class that holds the fields. Third, the same factory style with fromJson, which adds json_serializable:

// lib/order_freezed.dart
import 'package:freezed_annotation/freezed_annotation.dart';

part 'order_freezed.freezed.dart';
part 'order_freezed.g.dart';

@freezed
abstract class Order with _$Order {
  const factory Order({
    required String id,
    required int quantity,
    double? discount,
    @Default(<String>[]) List<String> tags,
  }) = _Order;

  factory Order.fromJson(Map<String, Object?> json) => _$OrderFromJson(json);
}

Notice that the default value moves into an @Default annotation in the factory style. That is because Dart does not allow default values on redirecting factory constructors, while a primary constructor takes a normal = const [].

How the Build Time Benchmark Was Run

A generator script writes N model files per package, one class per file, with six fields each: two strings, an int, a nullable double, a DateTime and a List<String>. In addition, each model from the second one on has a nullable related field pointing at the previous model, so the models import each other like a real domain layer does. The full script is gen.sh in the benchmark folder; here is how the twelve packages were created:

# 4 variants x 3 sizes = 12 packages, each with its own pubspec.yaml
for n in 50 200 500; do
  for v in plain freezed_factory freezed_primary freezed_json; do
    bash gen.sh $v $n ${v}_$n && (cd ${v}_$n && dart pub get)
  done
done

Then bench.sh runs five rounds. Each round visits every package in turn, so a burst of background activity on the desktop hits all variants instead of one. These are the scenarios it times, trimmed to the timing lines:

# 1. cold: `clean` deletes the compiled build script and the asset graph
dart run build_runner clean
dart run build_runner build --delete-conflicting-outputs

# 2. no-op: nothing changed since the last build
dart run build_runner build --delete-conflicting-outputs

# 3. full: compiled script kept, asset graph and generated files removed
rm -f .dart_tool/build/asset_graph.json lib/models/*.freezed.dart lib/models/*.g.dart
dart run build_runner build --delete-conflicting-outputs

# 4. one edit: add `String? subtitle` to the middle model, rebuild, then restore
# 5. analyzer over the whole package, generated code included
dart analyze

A second script, bench_leaf.sh, adds two more scenarios after the first finished. It edits the last model, which nothing imports, and it repeats the cold and full builds with --force-jit. Wall time comes from date +%s%N around each command, so it includes process startup. Every run is written to results.csv as variant,models,scenario,round,wall_ms,reported. These two short scripts turn that file, and the packages themselves, into the tables below:

# summarize.sh: median of 5 runs (ms) per scenario, variant and size, plus all raw values
awk -F, 'NR > 1 { key = $3 "|" $1 "|" $2; vals[key] = vals[key] " " $5 }
END {
  for (k in vals) {
    n = split(substr(vals[k], 2), a, " ")
    for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) if (a[j] < a[i]) { t = a[i]; a[i] = a[j]; a[j] = t }
    split(k, p, "|")
    printf "%-10s %-16s %4s  median %6d ms   runs:%s\n", p[1], p[2], p[3], a[3], vals[k]
  }
}' results.csv | sort -k1,1 -k2,2 -k3,3n

# count_lines.sh: non-blank hand-written vs generated lines per package
printf '%-22s %10s %10s %10s\n' package hand_lines gen_lines gen_files
for d in plain_* freezed_*; do
  hand=$(find "$d/lib/models" -name '*.dart' ! -name '*.freezed.dart' ! -name '*.g.dart' -exec cat {} + | grep -cv '^[[:space:]]*$')
  gen=$(find "$d/lib/models" \( -name '*.freezed.dart' -o -name '*.g.dart' \) -exec cat {} + 2>/dev/null | grep -cv '^[[:space:]]*$')
  files=$(find "$d/lib/models" \( -name '*.freezed.dart' -o -name '*.g.dart' \) | wc -l)
  printf '%-22s %10s %10s %10s\n' "$d" "$hand" "$gen" "$files"
done

Cold Builds: Where the 22 Seconds Go

Here is the raw output for cold builds, with the default AOT compile and with JIT:

bash summarize.sh | grep -E '^cold'
cold       freezed_factory    50  median  22657 ms   runs: 24209 22000 21875 26296 22657
cold       freezed_factory   200  median  24452 ms   runs: 32107 22525 21837 28581 24452
cold       freezed_factory   500  median  24869 ms   runs: 24869 23512 24650 35393 30994
cold       freezed_json       50  median  23558 ms   runs: 25092 22404 22193 28795 23558
cold       freezed_json      200  median  25339 ms   runs: 25339 23965 24878 44680 30713
cold       freezed_json      500  median  27768 ms   runs: 26204 25945 27768 34028 33417
cold       freezed_primary    50  median  22596 ms   runs: 24081 21839 21873 28059 22596
cold       freezed_primary   200  median  23595 ms   runs: 23595 22089 22209 27596 27360
cold       freezed_primary   500  median  23913 ms   runs: 23913 22279 22882 30536 28628
cold_jit   freezed_factory    50  median   7857 ms   runs: 10153 9143 7784 7837 7857
cold_jit   freezed_factory   200  median  10264 ms   runs: 12242 11596 10264 9649 9800
cold_jit   freezed_factory   500  median  13165 ms   runs: 16308 13165 12550 12628 13331
cold_jit   freezed_json       50  median   9577 ms   runs: 11307 11009 8934 9577 9540
cold_jit   freezed_json      200  median  12988 ms   runs: 16676 12988 12339 12683 13132
cold_jit   freezed_json      500  median  18424 ms   runs: 25157 17823 18424 18068 18634
cold_jit   freezed_primary    50  median   8182 ms   runs: 9820 8858 7531 8182 8166
cold_jit   freezed_primary   200  median   9969 ms   runs: 10976 10985 8982 8993 9969
cold_jit   freezed_primary   500  median  12317 ms   runs: 15607 11278 11849 12317 12600

The striking part is how flat the AOT rows are. Going from 50 to 500 models adds only 1.3 to 4.2 seconds to a cold build. In other words, nearly all of a cold build is fixed cost, and the model count barely matters.

That fixed cost has a cause. The build_runner 2.14.0 changelog says it now defaults “to AOT compilation for commands other than run. This costs more initial startup time but gives faster builds afterwards.” The build log confirms it, since every line ends in Built with build_runner/aot. For comparison, the Riverpod codegen measurement on this same PC used build_runner 2.7.1, before that change, and its cold builds took 7.4 seconds.

Should CI Use –force-jit?

Yes, if your CI starts from a clean checkout every time. The JIT compile is much cheaper, so a cold JIT build of 500 freezed models finished in 13.2 seconds against 24.9 seconds with AOT. For the JSON variant, it was 18.4 against 27.8 seconds. That is 9 to 15 seconds saved on every pipeline run that executes build_runner.

The catch is what happens after the compile. Here are the full regenerations, with the build script already compiled:

bash summarize.sh | grep -E '^full'
full       freezed_factory    50  median   1567 ms   runs: 1361 2456 2506 1567 1363
full       freezed_factory   200  median   2461 ms   runs: 3171 3094 1962 2461 2007
full       freezed_factory   500  median   4165 ms   runs: 4338 4356 3343 3715 4165
full       freezed_json       50  median   1635 ms   runs: 1686 1626 1603 1905 1635
full       freezed_json      200  median   3660 ms   runs: 2941 4048 4400 3512 3660
full       freezed_json      500  median   6027 ms   runs: 5777 5751 7740 6027 7328
full       freezed_primary    50  median   1464 ms   runs: 1258 2381 2383 1464 1305
full       freezed_primary   200  median   1989 ms   runs: 1584 2741 2819 1989 1925
full       freezed_primary   500  median   2753 ms   runs: 3422 2351 3426 2452 2753
full_jit   freezed_factory    50  median   3784 ms   runs: 4823 4267 3718 3720 3784
full_jit   freezed_factory   200  median   6709 ms   runs: 6919 7067 6709 5601 5725
full_jit   freezed_factory   500  median   9925 ms   runs: 11948 9925 8730 8887 10115
full_jit   freezed_json       50  median   6041 ms   runs: 6678 6303 4791 6041 6015
full_jit   freezed_json      200  median   8612 ms   runs: 10876 8612 8297 8608 9765
full_jit   freezed_json      500  median  15481 ms   runs: 19408 14420 15534 14290 15481
full_jit   freezed_primary    50  median   4177 ms   runs: 4177 4064 3483 4631 4639
full_jit   freezed_primary   200  median   6178 ms   runs: 6178 6194 4967 5006 6214
full_jit   freezed_primary   500  median   8508 ms   runs: 11398 7675 8480 8545 8508

With a compiled script, AOT regenerates 500 factory-style models in 4.2 seconds, while JIT takes 9.9. So AOT is the right default on a developer machine, where one compile serves hundreds of rebuilds. On a fresh CI runner, by contrast, there is only one build, so the compile never pays off.

Generation Cost per 100 Models

Taking the slope between the 50 and 500 model medians gives the marginal cost of code generation itself, separate from startup. With the AOT script warm, it works out to about 0.29 seconds per 100 models for freezed on primary constructors, 0.58 seconds for factory style and 0.98 seconds with JSON. Under JIT, the same slopes are 0.96, 1.36 and 2.10 seconds. These are derived from the medians above, not separately measured.

Warm Builds: One Edit, a Leaf Edit and No-Op

Day to day, the number that matters is how long a rebuild takes after you change one model. The answer depends on where that model sits in the import graph:

bash summarize.sh | grep -E '^(one_edit|leaf_edit|noop)'
leaf_edit  freezed_factory    50  median   1375 ms   runs: 1383 1375 1383 1292 1287
leaf_edit  freezed_factory   200  median   1400 ms   runs: 1443 1450 1400 1289 1351
leaf_edit  freezed_factory   500  median   1620 ms   runs: 1661 1620 1584 1455 1633
leaf_edit  freezed_json       50  median   1503 ms   runs: 1514 1528 1503 1395 1342
leaf_edit  freezed_json      200  median   2018 ms   runs: 2080 1972 2018 1981 2116
leaf_edit  freezed_json      500  median   3116 ms   runs: 3097 3116 3164 3041 3169
leaf_edit  freezed_primary    50  median   1336 ms   runs: 1336 1367 1336 1231 1223
leaf_edit  freezed_primary   200  median   1394 ms   runs: 1450 1456 1394 1297 1378
leaf_edit  freezed_primary   500  median   1620 ms   runs: 1686 1620 1619 1490 1686
noop       freezed_factory    50  median   1291 ms   runs: 1126 2238 2227 1291 1140
noop       freezed_factory   200  median   1297 ms   runs: 2339 2284 1171 1297 1179
noop       freezed_factory   500  median   1461 ms   runs: 2366 2383 1327 1315 1461
noop       freezed_json       50  median   1184 ms   runs: 1176 1189 1168 1330 1184
noop       freezed_json      200  median   1487 ms   runs: 1261 2378 2513 1487 1446
noop       freezed_json      500  median   1516 ms   runs: 1516 1512 2641 1513 1728
noop       freezed_primary    50  median   1316 ms   runs: 1138 2246 2256 1316 1132
noop       freezed_primary   200  median   1408 ms   runs: 1158 2292 2284 1408 1316
noop       freezed_primary   500  median   1464 ms   runs: 2350 1260 2359 1260 1464
one_edit   freezed_factory    50  median   1438 ms   runs: 1264 2396 2369 1438 1278
one_edit   freezed_factory   200  median   1910 ms   runs: 2731 2728 1607 1910 1613
one_edit   freezed_factory   500  median   2712 ms   runs: 3415 3408 2272 2580 2712
one_edit   freezed_json       50  median   1473 ms   runs: 1473 1456 1466 1699 1476
one_edit   freezed_json      200  median   2831 ms   runs: 2304 3462 3691 2831 2745
one_edit   freezed_json      500  median   4120 ms   runs: 4085 4079 5600 4120 5154
one_edit   freezed_primary    50  median   1393 ms   runs: 1215 2393 2326 1393 1218
one_edit   freezed_primary   200  median   1823 ms   runs: 1433 2545 2690 1823 1658
one_edit   freezed_primary   500  median   2122 ms   runs: 2983 1846 3118 1883 2122

A no-op build costs about 1.2 to 1.5 seconds regardless of size, and that is the floor for any build_runner build from a fresh process. Editing a leaf model, one that nothing imports, adds very little on top of it for freezed alone. With JSON at 500 models, however, a leaf edit still took 3.1 seconds.

Why Editing One Model Rebuilt 250 Files

The middle-model edit is where the import graph shows. build_runner reports how many files it wrote, and in round one at 500 models the log said:

### freezed_factory 500 one_edit round 1: 3415 ms
  Built with build_runner/aot in 2s; wrote 250 outputs.
### freezed_json 500 one_edit round 1: 4085 ms
  Built with build_runner/aot in 3s; wrote 750 outputs.
### freezed_factory 500 leaf_edit round 1: 1661 ms
  Built with build_runner/aot in 1s; wrote 1 output.
### freezed_json 500 leaf_edit round 1: 3097 ms
  Built with build_runner/aot in 2s; wrote 3 outputs.

Changing model 250 regenerated models 250 through 499, because each of those imports the previous one, directly or transitively. Freezed resolves the types of every field, so a change anywhere upstream reruns the builder downstream. The benchmark chains models on purpose, which makes this close to a worst case. Even so, a real app with a barrel file such as models.dart that every model imports behaves the same way: touch one file and every model behind the barrel reruns.

Generated Lines and the Analyzer Bill

Build time is only half the cost of code generation. The other half is that every generated line is code your IDE analyzes on each keystroke. Here is the line count across all twelve packages:

bash count_lines.sh | sort
package                hand_lines  gen_lines  gen_files
freezed_factory_200          2998      53771        200
freezed_factory_50            748      13421         50
freezed_factory_500          7498     134471        500
freezed_json_200             3398      61167        400
freezed_json_50               848      15267        100
freezed_json_500             8498     152967       1000
freezed_primary_200          2598      15386        200
freezed_primary_50            648       3836         50
freezed_primary_500          6498      38486        500
plain_200                    1998          0          0
plain_50                      498          0          0
plain_500                    4998          0          0

At 500 models, factory style produced 134,471 generated lines, while freezed on primary constructors produced 38,486. The difference comes from what freezed has to generate. In factory style, it writes a private _Model class that holds the fields, its own copyWith implementation, and a Model010Patterns extension with map, maybeMap, mapOrNull, when, maybeWhen and whenOrNull. With a primary constructor, Dart already declares the fields, so freezed only emits the mixin with copyWith, ==, hashCode and toString.

That difference shows up directly in analyzer time:

bash summarize.sh | grep -E '^analyze'
analyze    freezed_factory    50  median    987 ms   runs: 1403 987 901 983 1479
analyze    freezed_factory   200  median   2001 ms   runs: 2712 2001 1904 1929 3061
analyze    freezed_factory   500  median   4326 ms   runs: 6102 4326 3602 41943 3550
analyze    freezed_json       50  median   1067 ms   runs: 2004 940 926 1067 1479
analyze    freezed_json      200  median   2262 ms   runs: 2841 2262 2118 14734 1938
analyze    freezed_json      500  median   5230 ms   runs: 6109 4482 5230 17660 4020
analyze    freezed_primary    50  median   1001 ms   runs: 1001 906 803 1041 1398
analyze    freezed_primary   200  median   1620 ms   runs: 1917 1369 1550 1620 3086
analyze    freezed_primary   500  median   2878 ms   runs: 3166 2612 2752 8087 2878
analyze    plain              50  median    806 ms   runs: 930 793 739 806 1448
analyze    plain             200  median    910 ms   runs: 1700 910 849 886 2077
analyze    plain             500  median   1671 ms   runs: 2450 1319 1385 8799 1671

The fourth round has large outliers across several rows (41,943 ms for one factory run), which points to something else on the desktop competing for the disk or CPU at that moment. The medians ignore it, and it is left in the raw output on purpose. Either way, the ordering holds in every round: plain is fastest, then freezed on primary constructors, then factory style, then JSON.

Does Freezed on a Primary Constructor Keep Lists Immutable?

No, and this is the trade-off the build numbers hide. With the factory style, freezed owns the fields, so it wraps every List, Map and Set in an unmodifiable view. With a real primary constructor, however, Dart owns the field and freezed cannot intercept the assignment. This check runs all four order classes from earlier:

// bin/check.dart
import 'dart:convert';

import 'package:check/order_freezed.dart';
import 'package:check/order_handwritten.dart';
import 'package:check/order_plain.dart';

void main() {
  final p1 = PlainOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  final p2 = PlainOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  print('plain      == : ${p1 == p2}');
  print('plain      toString: $p1');

  final h1 = HandOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  final h2 = HandOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  print('handwritten == : ${h1 == h2}');
  print('handwritten copyWith(discount: null).discount: ${h1.copyWith(discount: null).discount}');

  final f1 = Order(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  final f2 = Order(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  print('freezed    == : ${f1 == f2}');
  print('freezed    toString: $f1');
  print('freezed    copyWith(discount: null).discount: ${f1.copyWith(discount: null).discount}');
  final json = jsonEncode(f1.toJson());
  print('freezed    toJson: $json');
  print('freezed    round trip equal: ${Order.fromJson(jsonDecode(json) as Map<String, Object?>) == f1}');
  try {
    f1.tags.add('x');
  } on UnsupportedError catch (e) {
    print('freezed    tags.add: UnsupportedError (${e.message})');
  }
}
// bin/check_primary.dart
import 'package:check/order_freezed_primary.dart';

void main() {
  final a = PrimaryOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  final b = PrimaryOrder(id: 'A-1', quantity: 2, discount: 0.1, tags: ['gift']);
  print('freezed primary == : ${a == b}');
  print('freezed primary toString: $a');
  print('freezed primary copyWith(discount: null).discount: ${a.copyWith(discount: null).discount}');
  a.tags.add('rush'); // no exception: Dart owns this field, not freezed
  print('freezed primary tags after add: ${a.tags}');
  print('freezed primary == after mutation: ${a == b}');
}

Here is the raw output of the two check scripts, dart run bin/check.dart followed by dart run bin/check_primary.dart:

plain      == : false
plain      toString: Instance of 'PlainOrder'
handwritten == : true
handwritten copyWith(discount: null).discount: 0.1
freezed    == : true
freezed    toString: Order(id: A-1, quantity: 2, discount: 0.1, tags: [gift])
freezed    copyWith(discount: null).discount: null
freezed    toJson: {"id":"A-1","quantity":2,"discount":0.1,"tags":["gift"]}
freezed    round trip equal: true
freezed    tags.add: UnsupportedError (Cannot add to an unmodifiable list)
freezed primary == : true
freezed primary toString: PrimaryOrder(id: A-1, quantity: 2, discount: 0.1, tags: [gift])
freezed primary copyWith(discount: null).discount: null
freezed primary tags after add: [gift, rush]
freezed primary == after mutation: false

Three results matter here. First, the plain primary constructor gives identity equality and an unhelpful toString. Second, the hand-written copyWith cannot clear a nullable field, so the discount stayed at 0.1, while both freezed versions correctly return null. Third, the freezed primary-constructor model let tags.add('rush') through without complaint, and the object silently stopped being equal to its twin. In state management, that is the classic bug where a mutation never triggers a rebuild because the old and new state are the same list.

Is Freezed Still Worth Using With Dart 3.13?

Yes, for models that need value equality, copyWith or JSON, freezed still earns its place, and primary constructors do not replace it. They replace only the field declarations. Freezed still generates the parts that are tedious and error-prone by hand: deep equality on collections, a copyWith that can set null, and the JSON bridge.

What changed is the cost side. On build_runner 2.17, generating code for even 500 models takes a few seconds once the build script is compiled. The dominant cost is the 22-second compile on a cold build, which --force-jit cuts roughly in half in CI. Meanwhile, the analyzer cost scales with generated lines, and that is the strongest argument for writing new models on primary constructors rather than factory style.

A Realistic Migration: 150 Models and a Slow CI Step

Consider a mid-sized Flutter app with 20 to 30 screens and 100 to 200 freezed models, maintained by a small team over several years. The models are all factory style, most have fromJson, and they sit behind a single models.dart barrel. Two complaints come up regularly: the CI codegen step feels slow, and the IDE lags in the data layer.

Based on the measurements above, the CI complaint is mostly the AOT compile, not freezed. Adding --force-jit to the CI command is a one-line change, and at this size the cold JSON build dropped from 25.3 to 13.0 seconds at 200 models in this benchmark. That fix needs no model changes at all, so it comes first.

The IDE complaint is about generated lines. Converting factory-style models to primary constructors cuts generated code by roughly 70% per model, judging by the 269 versus 77 lines measured here without JSON. However, the conversion is not free. Every @Default becomes a parameter default and call sites keep the same named arguments, but any code that relies on List fields being unmodifiable loses that protection. A pragmatic plan converts models in the domain layer gradually, one feature at a time, and adds a lint or test for collection mutation before converting state classes. If the app separates layers as described in the Flutter clean architecture and dependency injection guide, the data-transfer models are the safest place to start.

When to Use Freezed or Primary Constructors

Pick freezed factory style when

  • State classes hold List, Map or Set fields and you rely on them being unmodifiable
  • You use sealed unions with several factory constructors, which primary constructors cannot express
  • You want the generated when and map helpers instead of Dart switch patterns
  • The model needs fromJson and you already pay the json_serializable cost

Choose freezed on a primary constructor when

  • You need ==, hashCode, copyWith and toString on a simple immutable model
  • Analyzer speed in a large data layer matters more than collection immutability
  • Collections are already wrapped in List.unmodifiable at the boundary, or the model has no collections

Keep plain primary constructors when

  • The class is a value holder that is never compared, copied or used as state
  • It is a service, repository or configuration object, not a data class
  • You want zero build step for a package that would otherwise need build_runner for one class

When NOT to Use Freezed

  • For two or three small models in a package that has no other code generation, since a cold build adds over 20 seconds to every clean CI run
  • For classes with behavior and mutable internal state, where @unfreezed fights the tool
  • When your team never uses copyWith or value equality on the model, so the generated code is pure overhead
  • In hot analyzer paths of a huge monorepo, where factory-style generated code multiplies analysis time

Common Mistakes with Freezed and Primary Constructors

  • Assuming freezed on a primary constructor still makes collections unmodifiable; the runtime check shows it does not
  • Writing copyWith by hand with ?? and later discovering it cannot set a nullable field back to null
  • Blaming freezed for slow CI when the 22-second cost is build_runner’s AOT compile of its own script
  • Running --force-jit locally as well, where AOT regenerated 2.4 to 3.7 times faster once compiled
  • Importing every model through one barrel file, so editing any model reruns the builder for all of them
  • Converting everything at once to primary constructors and losing @Default values that the factory style declared

What This Benchmark Does Not Cover

First, the models are synthetic. They have six fields and one relation each, and the chain of imports is deliberate, so a real app’s edit costs will fall somewhere between the leaf and middle-model numbers. Second, build_runner watch was not measured. Watch mode keeps the process warm, so it skips the roughly 1.2-second process floor that every build call pays, and teams running it all day will see faster per-save numbers.

Third, CI caching of .dart_tool/build was not tested. If your pipeline restores that folder and the build script digest matches, AOT may skip its compile, and then --force-jit would lose its advantage. Fourth, JIT was timed only for cold and full builds, not for edits. Finally, all of this ran on one Windows desktop with Dart 3.13.7. A later build_runner may change the AOT trade-off again, and this post will be re-run when it does.

Conclusion

Measured on Dart 3.13, freezed vs primary constructors is not an either-or choice. Primary constructors remove the field boilerplate, and freezed on top of them still supplies equality, copyWith and JSON, at 77 generated lines per model instead of 269 and a faster analyzer. However, that combination does not make collections unmodifiable, so keep factory style for state classes that hold lists and maps. As for build time, freezed itself costs about a second or less per 100 models, while the 22-second cold start belongs to build_runner’s AOT compile.

So today, add --force-jit to your CI codegen step, and write your next new model on a primary constructor with @freezed. Then, if your models live in a Riverpod or Bloc app, check whether any state class mutates a list before converting it. For the language changes that came with 3.13, the Dart language features overview is the next read.