You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/exercises/week-08/instructions.md
+58-49Lines changed: 58 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,110 +17,119 @@ You're working on a **Professional Book Library Dashboard** for a company's inte
17
17
18
18
**The Problem:** The current implementation has significant performance issues! Every user interaction causes unnecessary re-renders and expensive recalculations, making the interface sluggish and inefficient.
19
19
20
-
**Your Mission:** Optimize the application using `useMemo` and `useCallback` hooks to improve performance while maintaining all functionality. You'll use built-in render counters to measure your improvements.
20
+
**Your Mission:** Optimize the application using `useMemo` and `useCallback` hooks to improve performance while maintaining all functionality. You'll use built-in render counters and **performance timing benchmarks**to measure your improvements.
21
21
22
22
**Current Features:**
23
23
24
-
- 📊 **Real-time Statistics**: Calculates averages, totals, and insights about the book collection
24
+
- 📊 **Real-time Statistics**: Calculates averages, totals, and insights about the book collection with **artificial computational load** for benchmarking
25
25
- 🔍 **Search Functionality**: Filter books by title or author
26
26
- 🏷️ **Genre Filtering**: Multi-select genre filters with visual indicators
27
27
- 📈 **Dynamic Sorting**: Sort by title, author, rating, publication year, or price
28
28
- ❤️ **Favorites System**: Add books to personal favorites list
29
29
- 📱 **Performance Monitoring**: Built-in render counters positioned in the upper-right corner of components show optimization impact
30
+
- ⏱️ **Timing Benchmarks**: Real-time calculation timing displayed in microseconds to demonstrate performance improvements
30
31
31
32
## Instructions
32
33
33
34
### Step 1: Explore the Performance Issues
34
35
35
36
1.**Start the application** and navigate to the Week 08 page
36
37
2.**Observe the render counters** - notice how they appear in the upper-right corner of each component showing render counts
37
-
3.**Test these interactions** and watch the render numbers:
38
+
3.**Notice the timing benchmarks** - BookStats displays calculation time in milliseconds and microseconds
39
+
4.**Test these interactions** and watch both render numbers and timing:
38
40
- Type in the search box (watch ALL components re-render)
39
41
- Click different sorting options (notice expensive calculations)
40
42
- Toggle genre filters (see unnecessary re-renders)
41
43
- Click "Add to Favorites" on any book (watch BookCard components re-render)
44
+
-**Pay special attention** to the BookStats timing display - you should see ~1-5ms calculation times
42
45
43
-
**Expected Issues:** You should see render counts rapidly increasing with every interaction, indicating performance problems.
46
+
**Expected Issues:** You should see render counts rapidly increasing with every interaction, plus consistent calculation timing even when the underlying data hasn't changed.
### Step 2: Stabilize Search Handler Function (Priority: Immediate Impact)
46
49
47
-
**Location:**`BookStats` component
48
-
**Problem:** Statistics calculations run on every render, even when the books array hasn't changed.
50
+
**Location:**`StudentWork` (main component)
51
+
**Problem:**`handleSearch` function is recreated on every render, causing all BookCard components to re-render.
52
+
53
+
**TODO #1:** Find the `handleSearch` function and stabilize it with `useCallback`.
49
54
50
-
**TODO #1:**Find the `calculateStats()` function call and optimize it with `useMemo`.
55
+
**Why First:**This creates the most visible performance impact - every keystroke in search causes all BookCard components to unnecessarily re-render.
51
56
52
57
**Hints:**
53
58
54
-
- Look for where the statistics are being calculated in the component
55
-
- The calculation should only run when the `books` array changes
56
-
- Consider what should be in the dependency array
59
+
- Look for the function that handles the search input onChange event
60
+
- Consider what dependencies `useCallback` needs (likely none)
57
61
58
-
**Test:**After implementing, type in the search box and verify that `BookStats`render count stays low when only search term changes.
62
+
**Test:**Type in the search box and verify that individual BookCard render counts don't increase unnecessarily.
59
63
60
-
### Step 3: Optimize Expensive Sorting Operations
64
+
### Step 3: Stabilize Favorites Handler Function (Priority: High Impact)
61
65
62
-
**Location:**`BookList` component
63
-
**Problem:** The sorting operation runs on every render, even when the books or sortBy haven't changed.
66
+
**Location:**`StudentWork` (main component)
67
+
**Problem:**`handleToggleFavorite` function is recreated on every render, causing BookCard re-renders.
68
+
69
+
**TODO #2:** Find the `handleToggleFavorite` function and stabilize it with `useCallback`.
64
70
65
-
**TODO #2:**Find the sorting logic and optimize it with `useMemo`.
71
+
**Why Second:**This prevents cascading re-renders when managing favorites - currently clicking one favorite causes all BookCard components to re-render.
66
72
67
73
**Hints:**
68
74
69
-
- Look for the `sortedBooks` variable assignment
70
-
- The sorting operation includes a switch statement with different sort criteria
71
-
- Consider what values should trigger a re-sort (books array, sort criteria)
72
-
- Make sure to include all necessary dependencies in the dependency array
75
+
- Look for the function that handles adding/removing favorites
76
+
- It takes a bookId parameter and toggles the book's favorite status
77
+
- Consider what dependencies `useCallback` needs (likely none)
73
78
74
-
**Test:**Change the sort option and verify that sorting only happens when `sortBy` changes, not on every render.
79
+
**Test:**Click "Add to Favorites" and verify that other BookCard components don't re-render unnecessarily.
75
80
76
-
### Step 4: Stabilize Search Handler Function
81
+
### Step 4: Optimize Expensive Sorting Operations
77
82
78
-
**Location:**`StudentWork` (main component)
79
-
**Problem:**`handleSearch` function is recreated on every render, causing all BookCard components to re-render.
83
+
**Location:**`BookList`component
84
+
**Problem:**The sorting operation runs on every render, even when the books or sortBy haven't changed.
80
85
81
-
**TODO #3:** Find the `handleSearch` function and stabilize it with `useCallback`.
86
+
**TODO #3:** Find the sorting logic and optimize it with `useMemo`.
87
+
88
+
**Why Third:** Once function references are stable, optimize the computational work.
82
89
83
90
**Hints:**
84
91
85
-
- Look for the function that handles the search input onChange event
86
-
- This function updates the search term state
87
-
- Consider what dependencies this function actually needs (likely none)
88
-
- The function is passed down to child components, so stabilizing it prevents unnecessary re-renders
92
+
- Look for the `sortedBooks` variable assignment
93
+
- Consider what values should trigger a re-sort (books array, sort criteria)
94
+
- Make sure to include all necessary dependencies in the dependency array
89
95
90
-
**Test:**Type in the search box and verify that individual BookCard render counts don't increase unnecessarily.
96
+
**Test:**Change the sort option and verify that sorting only happens when `sortBy` changes, not on every render.
**Problem:**`handleToggleFavorite` function is recreated on every render, causing BookCard re-renders.
100
+
**Location:**`BookStats`component
101
+
**Problem:**Statistics calculations run on every render, even when the books array hasn't changed.
96
102
97
-
**TODO #4:** Find the `handleToggleFavorite` function and stabilize it with `useCallback`.
103
+
**TODO #4:** Find the `calculateStats()` function call and optimize it with `useMemo`.
98
104
99
-
**Hints:**
105
+
**Why Last:** After fixing the render cascade issues, optimize the remaining expensive calculations.
100
106
101
-
- Look for the function that handles adding/removing favorites
102
-
- This function updates the favorites state array
103
-
- It takes a bookId parameter and toggles the book's favorite status
104
-
- Consider what dependencies this function needs (likely none since it uses the updater function pattern)
105
-
- This function is passed to each BookCard component
107
+
**Performance Note:** The statistics calculations include artificial computational load (20,000 iterations of mathematical operations) to make performance improvements clearly visible through timing benchmarks. This simulates real-world expensive calculations like complex data processing or API transformations.
106
108
107
-
**Test:** Click "Add to Favorites" and verify that other BookCard components don't re-render unnecessarily.
109
+
**Hints:**
108
110
109
-
### Step 6: Import Required Hooks
111
+
- Look for where the statistics are being calculated in the component
112
+
- The calculation should only run when the `books` array changes
113
+
- Consider what should be in the dependency array
114
+
-**Watch the timing display** - you should see calculation time drop to nearly 0ms on subsequent renders when `books` hasn't changed
110
115
111
-
**Don't forget:**You'll need to import the optimization hooks you're using. Check the import statement at the top of each file and add `useMemo` and `useCallback` to your React imports where needed.
116
+
**Test:**After implementing, type in the search box and verify that:
112
117
113
-
**Hint:** Look for the existing `import { useState } from 'react';` line and add the additional hooks you're implementing.
118
+
-`BookStats` render count stays low when only search term changes
119
+
-**Timing benchmarks show ~0.00ms** for cached calculations vs. ~1-5ms for fresh calculations
120
+
- Console logs show "Stats calculation took: 0.0000ms" for memoized results
114
121
115
122
## Assessment Criteria
116
123
117
-
**Performance Improvements (Measure with Render Counters):**
124
+
**Performance Improvements (Measure with Render Counters and Timing Benchmarks):**
118
125
119
126
-[ ] BookStats component renders ≤ 2 times when typing in search (was ~10+ times)
127
+
-[ ]**BookStats calculation timing shows ~0.00ms for memoized results** (was ~1-5ms every render)
120
128
-[ ] BookList component renders only when books/sorting changes (was every keystroke)
121
129
-[ ] Individual BookCard components don't re-render when other BookCards are favorited
122
130
-[ ] Search input typing doesn't cause all BookCard components to re-render
123
131
-[ ] Sorting operations only occur when sort option actually changes
132
+
-[ ]**Console logs demonstrate performance improvements** with timing comparisons
124
133
125
134
**Functionality Requirements:**
126
135
@@ -130,21 +139,21 @@ You're working on a **Professional Book Library Dashboard** for a company's inte
130
139
-[ ] Favorites can be added and favorites count updates correctly
131
140
-[ ] Statistics display correctly and update when filters change
132
141
-[ ] All visual render counters are visible and functional
142
+
-[ ]**Timing benchmarks display correctly** in BookStats component
133
143
134
144
**Code Quality:**
135
145
136
146
-[ ] Proper `useMemo` dependency arrays include all relevant dependencies
137
147
-[ ] Proper `useCallback` dependency arrays (empty for these specific cases)
138
148
-[ ] All original functionality preserved after optimization
139
149
-[ ] Console shows reduced render logging after optimization
150
+
-[ ]**Performance improvements are measurable** through timing benchmarks
140
151
-[ ] Code is clean and follows the existing patterns
141
152
142
153
## Proof of Completion
143
154
144
-
1.**Take a screenshot** showing the optimized application with low render counts while interacting with the interface
145
-
2.**Include your screenshot** in your pull request description
146
-
3.**Commit your changes** with a descriptive message: `"Week-08: Optimized Book Dashboard with useMemo and useCallback"`
147
-
4.**Create a pull request** with your week-08 branch
155
+
1.**Commit your changes** with a descriptive message: `"Week-08: Optimized Book Dashboard with useMemo and useCallback"`
156
+
2.**Create a pull request** with your week-08 branch
0 commit comments