0Pricing
Swift Academy · Lesson

Lazy vs Eager Trade-offs

Know when laziness helps or hurts.

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

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