Swift Academy · Lesson

Lazy vs Eager Trade-offs

Know when laziness helps or hurts.

Lesson 3 of 413 steps

Lazy vs Eager Trade-offs is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

No Free Lunch

Lazy is not always faster. It trades upfront allocation for per-access overhead and changes when side effects run. Knowing the trade-offs lets you pick correctly.

Lazy Wins: Partial Consumption

Only a slice of a huge transform is needed — lazy skips the rest:

let data = Array(1...1_000_000)
let top = data.lazy.map { $0 * 2 }.prefix(3)
print(Array(top))  // [2, 4, 6]  -- 999,997 transforms skipped

Eager Wins: Full Reuse

If you iterate the result many times, eager computes once; lazy recomputes every pass:

let doubled = [1, 2, 3].map { $0 * 2 }  // computed once
print(doubled.reduce(0, +))  // 12
print(doubled.max()!)        // 6  -- no recompute

Lazy Recomputes

The same lazy view does the work again on each iteration:

var work = 0
let view = [1, 2, 3].lazy.map { (n: Int) -> Int in work += 1; return n }
_ = view.reduce(0, +)
_ = view.max()
print(work)  // 6  -- transformed twice

Overhead Per Element

Each lazy step wraps the sequence in another type and calls through closures element by element. For tiny arrays that overhead can outweigh the saved allocation.

Small Array, Eager Is Fine

For a handful of elements consumed fully, prefer plain eager — it is simpler and the allocation is trivial:

let nums = [3, 1, 2]
let sorted = nums.map { $0 + 1 }.sorted()
print(sorted)  // [2, 3, 4]

Side-Effect Timing Differs

With lazy, the closure body runs later than the line that defines it. Avoid relying on side effects inside lazy transforms:

var log: [Int] = []
let v = [1, 2].lazy.map { (n: Int) -> Int in log.append(n); return n }
print(log)        // []  -- nothing ran yet
_ = Array(v)
print(log)        // [1, 2]

Operations That Force Eager

Some operations must read the whole sequence anyway, so lazy gives no benefit there:

let nums = Array(1...10)
print(nums.lazy.map { $0 * 2 }.sorted())  // sorted must see all -> [2,4,...,20]
print(nums.lazy.map { $0 }.count)         // count walks everything

Lazy plus contains

Short-circuiting operations like contains(where:) benefit from lazy on big inputs:

let big = Array(1...1_000_000)
let found = big.lazy.map { $0 * 2 }.contains { $0 == 8 }
print(found)  // true  -- stops at element 4

A Decision Checklist

Use lazy when: source is large AND you keep only part (prefix / first / contains) AND you iterate once. Use eager when: array is small, you reuse the result, or readability matters more than micro-optimization.

let huge = Array(1...100_000)
let first = huge.lazy.filter { $0 % 9973 == 0 }.first
print(first!)  // 9973

Measure, Do Not Guess

Performance intuition is often wrong. When it matters, benchmark both versions with realistic data before committing to lazy.

let nums = Array(1...50)
let eager = nums.filter { $0 > 25 }.count
let lazyC = nums.lazy.filter { $0 > 25 }.count
print(eager == lazyC)  // true -- same answer, profile for speed

Quick Check

When is eager the better choice over lazy?

Recap

You learned the trade-offs:

  • Lazy wins on large sources with partial, single-pass consumption
  • Eager wins for small arrays and reused results (lazy recomputes)
  • Side effects in lazy transforms run later — avoid depending on them
  • Measure before optimizing

Next: building custom lazy sequences.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Lazy vs Eager Trade-offs” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Lazy vs Eager Trade-offs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Lazy vs Eager Trade-offs”?

Know when laziness helps or hurts. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Lazy vs Eager Trade-offs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The lazy Property
  2. Lazy map and filter
  3. Lazy vs Eager Trade-offs
  4. Building Custom Lazy Sequences
← Back to Swift Academy