Skip to content

Commit 62a7790

Browse files
committed
update instructions
1 parent 5c876d1 commit 62a7790

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

src/exercises/week-08/instructions.md

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,110 +17,119 @@ You're working on a **Professional Book Library Dashboard** for a company's inte
1717

1818
**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.
1919

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.
2121

2222
**Current Features:**
2323

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
2525
- 🔍 **Search Functionality**: Filter books by title or author
2626
- 🏷️ **Genre Filtering**: Multi-select genre filters with visual indicators
2727
- 📈 **Dynamic Sorting**: Sort by title, author, rating, publication year, or price
2828
- ❤️ **Favorites System**: Add books to personal favorites list
2929
- 📱 **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
3031

3132
## Instructions
3233

3334
### Step 1: Explore the Performance Issues
3435

3536
1. **Start the application** and navigate to the Week 08 page
3637
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:
3840
- Type in the search box (watch ALL components re-render)
3941
- Click different sorting options (notice expensive calculations)
4042
- Toggle genre filters (see unnecessary re-renders)
4143
- 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
4245

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.
4447

45-
### Step 2: Optimize Expensive Statistics Calculations
48+
### Step 2: Stabilize Search Handler Function (Priority: Immediate Impact)
4649

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`.
4954

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.
5156

5257
**Hints:**
5358

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)
5761

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.
5963

60-
### Step 3: Optimize Expensive Sorting Operations
64+
### Step 3: Stabilize Favorites Handler Function (Priority: High Impact)
6165

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`.
6470

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.
6672

6773
**Hints:**
6874

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)
7378

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.
7580

76-
### Step 4: Stabilize Search Handler Function
81+
### Step 4: Optimize Expensive Sorting Operations
7782

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.
8085

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.
8289

8390
**Hints:**
8491

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
8995

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.
9197

92-
### Step 5: Stabilize Favorites Handler Function
98+
### Step 5: Optimize Expensive Statistics Calculations
9399

94-
**Location:** `StudentWork` (main component)
95-
**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.
96102

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`.
98104

99-
**Hints:**
105+
**Why Last:** After fixing the render cascade issues, optimize the remaining expensive calculations.
100106

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.
106108

107-
**Test:** Click "Add to Favorites" and verify that other BookCard components don't re-render unnecessarily.
109+
**Hints:**
108110

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
110115

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:
112117

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
114121

115122
## Assessment Criteria
116123

117-
**Performance Improvements (Measure with Render Counters):**
124+
**Performance Improvements (Measure with Render Counters and Timing Benchmarks):**
118125

119126
- [ ] 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)
120128
- [ ] BookList component renders only when books/sorting changes (was every keystroke)
121129
- [ ] Individual BookCard components don't re-render when other BookCards are favorited
122130
- [ ] Search input typing doesn't cause all BookCard components to re-render
123131
- [ ] Sorting operations only occur when sort option actually changes
132+
- [ ] **Console logs demonstrate performance improvements** with timing comparisons
124133

125134
**Functionality Requirements:**
126135

@@ -130,21 +139,21 @@ You're working on a **Professional Book Library Dashboard** for a company's inte
130139
- [ ] Favorites can be added and favorites count updates correctly
131140
- [ ] Statistics display correctly and update when filters change
132141
- [ ] All visual render counters are visible and functional
142+
- [ ] **Timing benchmarks display correctly** in BookStats component
133143

134144
**Code Quality:**
135145

136146
- [ ] Proper `useMemo` dependency arrays include all relevant dependencies
137147
- [ ] Proper `useCallback` dependency arrays (empty for these specific cases)
138148
- [ ] All original functionality preserved after optimization
139149
- [ ] Console shows reduced render logging after optimization
150+
- [ ] **Performance improvements are measurable** through timing benchmarks
140151
- [ ] Code is clean and follows the existing patterns
141152

142153
## Proof of Completion
143154

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
148157

149158
## Reminders
150159

0 commit comments

Comments
 (0)