Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions PERFORMANCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Performance Optimization Report

Profiling was performed in **development mode** using the React `<Profiler>` API (same metrics as React DevTools Profiler: `actualDuration` = render duration, `commitTime - startTime` = commit duration). Each interaction was recorded in a separate session after the initial data load completed.

## Optimizations Applied

| Technique | Where applied |
| --------- | ------------- |
| `useMemo` | `filteredCountries`, `years`, `availableColumns`, `yearDataMap`, `population`, `co2`, `record` |
| `useCallback` | All event handlers in `App` (`handleSearch`, `handleYearChange`, `handleSortFieldChange`, etc.) |
| `React.memo` | `CountryList`, `CountryCard`, `DataTable`, `SearchBar`, `YearSelector`, `ColumnModal` |
| Proper `key` props | `country.id` for list items, `column` for table rows, `yearOption` for year options |
| Virtualization | `react-window` `List` component in `CountryList` (renders only visible rows) |

---

## Baseline Measurements

### Interaction A: Sort countries

- **Commit duration**: 0.89 s
- **Render duration**: 882.2 ms
- **Screenshot**: ![Sort countries baseline](screenshots/baseline/sort-countries.png)

> Toggling sort order re-rendered all ~250 `CountryCard` components. The flame chart showed `CountryList` → `CountryCard` → `DataTable` as the dominant render path.

### Interaction B: Search countries

- **Commit duration**: 0.12 s
- **Render duration**: 118.7 ms
- **Screenshot**: ![Search countries baseline](screenshots/baseline/search-countries.png)

> Typing "United" still triggered a full list reconciliation because `CountryCard` components used array index as `key`, causing unnecessary DOM updates even for filtered results.

### Interaction C: Change year

- **Commit duration**: 0.83 s
- **Render duration**: 822.3 ms
- **Screenshot**: ![Change year baseline](screenshots/baseline/change-year.png)

> Changing the year selector caused every visible country card and its data table to re-render, recalculating `createYearDataMap` on each card independently.

### Interaction D: Toggle column

- **Commit duration**: 1.20 s
- **Render duration**: 1188.5 ms
- **Screenshot**: ![Toggle column baseline](screenshots/baseline/toggle-column.png)

> Toggling a column in the modal re-rendered all country cards because handler references changed on every `App` render and components were not memoized.

---

## Optimized Measurements

### Interaction A: Sort countries

- **Commit duration**: 0.29 s
- **Render duration**: 294.1 ms
- **Screenshot**: ![Sort countries optimized](screenshots/optimized/sort-countries.png)

### Interaction B: Search countries

- **Commit duration**: 0.02 s
- **Render duration**: 18.0 ms
- **Screenshot**: ![Search countries optimized](screenshots/optimized/search-countries.png)

### Interaction C: Change year

- **Commit duration**: 0.04 s
- **Render duration**: 41.2 ms
- **Screenshot**: ![Change year optimized](screenshots/optimized/change-year.png)

### Interaction D: Toggle column

- **Commit duration**: 0.02 s
- **Render duration**: 19.3 ms
- **Screenshot**: ![Toggle column optimized](screenshots/optimized/toggle-column.png)

---

## Summary of Improvements

| Interaction | Baseline (ms) | Optimized (ms) | Improvement |
| ---------------- | ------------- | -------------- | ----------- |
| Sort countries | 882.2 | 294.1 | 66.7% |
| Search countries | 118.7 | 18.0 | 84.8% |
| Change year | 822.3 | 41.2 | 95.0% |
| Toggle column | 1188.5 | 19.3 | 98.4% |
| **Average** | **752.9** | **93.2** | **87.6%** |

### Key findings

1. **Virtualization** had the largest impact on sort and year-change interactions, reducing rendered nodes from ~250 cards to ~3–6 visible cards.
2. **`React.memo` + `useCallback`** prevented the entire component tree from re-rendering when toggling columns — only affected `CountryCard` instances updated.
3. **`useMemo`** for `filteredCountries` eliminated redundant filter/sort work and stabilized list references passed to the virtualized list.
4. **Stable `key` props** (`country.id` instead of array index) improved reconciliation when the filtered list changed during search.
38 changes: 23 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
},
"dependencies": {
"react": "^19.2.0",
"react-dom": "^19.2.0"
"react-dom": "^19.2.0",
"react-window": "^2.2.7"
},
"devDependencies": {
"@eslint/js": "^9.39.1",
"@types/node": "^24.10.1",
"@types/react": "^19.2.5",
"@types/react-dom": "^19.2.3",
"@types/react-window": "^1.8.8",
"@vitejs/plugin-react": "^5.1.1",
"eslint": "^9.39.1",
"eslint-plugin-react-hooks": "^7.0.1",
Expand Down
Binary file added screenshots/baseline/change-year.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/baseline/search-countries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/baseline/sort-countries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/baseline/toggle-column.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/optimized/change-year.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/optimized/search-countries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/optimized/sort-countries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/optimized/toggle-column.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 35 additions & 39 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useMemo, useCallback } from 'react';
import { useCo2Data } from '../../hooks/useCo2Data';
import { LoadingSpinner } from '../loading-spinner/loading-spinner';
import { SearchBar } from '../search-bar/search-bar';
Expand Down Expand Up @@ -32,40 +32,40 @@ export const App = () => {
isColumnModalOpen: false,
});

const years = data ? getAvailableYears(data) : [];
const availableColumns = getAvailableColumns();

const handleSearch = (value: string) => {
setState({ ...state, searchQuery: value });
};

const handleYearChange = (year: number) => {
setState({ ...state, selectedYear: year });
};

const handleSortFieldChange = (field: 'name' | 'population') => {
setState({ ...state, sortField: field });
};

const handleSortOrderToggle = () => {
setState({
...state,
sortOrder: state.sortOrder === 'asc' ? 'desc' : 'asc',
});
};

const handleColumnToggle = (column: string) => {
setState({
...state,
selectedColumns: state.selectedColumns.includes(column)
? state.selectedColumns.filter((c) => c !== column)
: [...state.selectedColumns, column],
});
};

const handleModalToggle = () => {
setState({ ...state, isColumnModalOpen: !state.isColumnModalOpen });
};
const years = useMemo(() => (data ? getAvailableYears(data) : []), [data]);
const availableColumns = useMemo(() => getAvailableColumns(), []);

const handleSearch = useCallback((value: string) => {
setState((prev) => ({ ...prev, searchQuery: value }));
}, []);

const handleYearChange = useCallback((year: number) => {
setState((prev) => ({ ...prev, selectedYear: year }));
}, []);

const handleSortFieldChange = useCallback((field: 'name' | 'population') => {
setState((prev) => ({ ...prev, sortField: field }));
}, []);

const handleSortOrderToggle = useCallback(() => {
setState((prev) => ({
...prev,
sortOrder: prev.sortOrder === 'asc' ? 'desc' : 'asc',
}));
}, []);

const handleColumnToggle = useCallback((column: string) => {
setState((prev) => ({
...prev,
selectedColumns: prev.selectedColumns.includes(column)
? prev.selectedColumns.filter((c) => c !== column)
: [...prev.selectedColumns, column],
}));
}, []);

const handleModalToggle = useCallback(() => {
setState((prev) => ({ ...prev, isColumnModalOpen: !prev.isColumnModalOpen }));
}, []);

if (isLoading) {
return <LoadingSpinner />;
Expand All @@ -83,7 +83,6 @@ export const App = () => {
<div className={styles.container}>
<h1 className={styles.title}>CO₂ Emissions Data Explorer</h1>

{/* Controls */}
<div className={styles.controls}>
<SearchBar value={state.searchQuery} onChange={handleSearch} />
<YearSelector year={state.selectedYear} years={years} onChange={handleYearChange} />
Expand Down Expand Up @@ -111,7 +110,6 @@ export const App = () => {
</div>
</div>

{/* Country List */}
<CountryList
countries={data}
searchQuery={state.searchQuery}
Expand All @@ -120,10 +118,8 @@ export const App = () => {
selectedYear={state.selectedYear}
sortField={state.sortField}
sortOrder={state.sortOrder}
onYearChange={handleYearChange}
/>

{/* Column Modal */}
<ColumnModal
isOpen={state.isColumnModalOpen}
availableColumns={availableColumns}
Expand Down
Loading