Lesson 5
Big O fundamentals
Build a practical mental model for big o fundamentals and use it in working JavaScript.
By the end, you will be able to…
- Explain big o fundamentals in your own words
- Use big o fundamentals in a small program
- Recognize and debug a common failure mode
Build the mental model
Big O describes how an algorithm's time or space grows as input size grows, ignoring constant factors. O(1) is constant, O(n) is linear, O(n²) is quadratic — the shape of growth is what matters, not one measurement.
Syntax and working example
Read the code from inputs to output. Predict what it prints, then run it and change one value.
Why this matters
It gives you a vocabulary to reason about whether an approach will still be fast enough at 10x or 1000x the data, before you find out the hard way in production.
Real-world example
Choosing a Set (O(1) lookup) over repeatedly scanning an array (O(n) lookup) inside a loop is a classic Big O-driven optimization.
Common mistakes
- Nesting a loop inside a loop over the same data, silently creating O(n²) behavior
- Optimizing constant factors while ignoring an algorithm's growth shape
- Assuming Big O measures actual wall-clock speed rather than growth rate
Try it yourself
Challenge
Identify and fix the accidental O(n²) pattern: check for duplicates using a Set instead of a nested loop.
Knowledge check
What does O(n²) typically indicate about an algorithm's structure?
Summary
Big O describes how work grows with input size — recognize O(1), O(n), and O(n²) patterns in your own code.