Solution
Tradeoffs
Every line of code is a choice. Learn to evaluate tradeoffs between readability, performance, and maintainability to write better JavaScript.
What Is a Tradeoff?
A tradeoff occurs whenever choosing one approach means giving up some benefit of another. In software, no solution is perfect on every dimension: gaining speed might cost clarity, and vice versa.
Why Tradeoffs Matter
Professional developers spend far more time reading code than writing it. A clever one-liner might run faster, but if teammates can't understand it, bugs multiply and progress slows.
That said, performance matters when your code runs in tight loops, handles large datasets, or powers real-time interfaces. The skill is knowing when each dimension matters most.
The Tradeoff Spectrum
The Core Principle
Measuring with Big-O
Big-O notation describes how execution time grows as input size \( n \) increases. Understanding it helps you compare approaches before writing a single benchmark.
Readability vs Performance
| Readable Approach | Performant Approach | |
|---|---|---|
| Goal | Clarity for humans | Speed for machines |
| Naming | Descriptive, verbose | Short, terse |
| Abstractions | Many small functions | Inlined logic |
| Data structures | Arrays, plain objects | Typed arrays, Maps, Sets |
| When to choose | Most application code | Hot paths, large data |
Decision Checklist
Example 1: Summing an Array
Example 2: Finding Duplicates
Example 3: A Real Tradeoff
Context Changes the Answer
The "right" answer depends on who reads the code, how much data flows through it, and how often it runs. There is no universal winner.
Maintainability: The Third Dimension
Maintainability is how easily your future self (or a colleague) can modify the code when requirements change. It overlaps with readability but also includes structure.
Refactoring for Clarity
Common Mistakes
Summary Table
| Factor | Readability | Performance | Maintainability |
|---|---|---|---|
| Priority | Default first | When measured | Always |
| Naming | Descriptive | Short OK | Consistent |
| Functions | Small, named | Inline if hot | Single purpose |
| Data structures | Simple | Specialised | Well-abstracted |
| Risk | Verbose code | Hard to debug | Over-engineering |
Question 1 of 5
Your team is building a to-do app with at most 100 items. A colleague writes a for-loop and you suggest .filter().map() instead. What is the best justification?
Question 2 of 5
Which approach has the worst Big-O complexity for checking if an array contains duplicates?
Question 3 of 5
A developer uses single-letter variable names and bitwise operators to make an animation loop run 5% faster. What should you tell them?
Question 4 of 5
Which of the following is NOT an example of improving readability?
.map() with a for-loop for marginal speed is a performance optimisation, not a readability improvement. Options A, B, and D all make code easier to understand.Question 5 of 5
You discover a function runs 500,000 times per page load and uses .filter().map() on a 10,000-element array. What is the best first step?