This repository demonstrates common memory leaks, inefficiencies, and performance issues in React applications. It serves as a practical tool to showcase how to identify, debug, and fix these problems using Chrome DevTools tools, including Lighthouse, Performance, Memory, Network, Record, Coverage, and Source tabs.
LeakyTimer.tsx
: Demonstrates a memory leak caused by an unclearedsetTimeout
.LeakyInterval.tsx
: Simulates a memory leak due to an unhandledsetInterval
.LeakyListener.tsx
: Illustrates a memory leak resulting from an unremovedresize
event listener on thewindow
object.
- Components like
RecipeCard.tsx
andIngredientCard.tsx
include inefficiencies such as redundant re-renders and unnecessary computations.
- Includes examples of:
- Unused CSS rules in components like
RecipeList.tsx
. - Unused functions or state variables across multiple components.
- Unused CSS rules in components like
- Components such as
RecipeHeader.tsx
,InstructionStep.tsx
, andRecipeBanner.tsx
highlight modularity and reusability, but also showcase potential inefficiencies.
git clone https://github.com/helabenkhalfallah/tools-devtools-app-analysis.git
cd tools-devtools-app-analysis
pnpm install
pnpm dev
This application allows you to:
-
Analyze Memory Leaks:
- Use the Memory tab to identify retained objects, detached DOM nodes, and closures.
-
Evaluate Application Performance:
- Use the Performance, Lighthouse, and Record tabs to analyze rendering times, bottlenecks, and resource usage.
-
Detect Unused Code:
- Use the Coverage and Source tabs to locate unused CSS and JavaScript.
-
Inspect Network Activity:
- Use the Network tab to debug API calls, analyze load times, and spot inefficient resource usage.
-
Analyze Bundle Size:
- Vite Bundler Analyzer (
pnpm analyze
): Visualize the size of the app’s modules to identify oversized dependencies or inefficient packaging. - Bundlephobia: Check the size, treeshaking support, and dependency impact of npm packages used in the app.
- Vite Bundler Analyzer (
-
Assess Webpage Loading:
- WebPageTest: Run comprehensive tests on your webpage to measure real-world performance, including loading speed, render times, and third-party resource impact.
Component | Issue | Description |
---|---|---|
LeakyTimer.tsx |
setTimeout not cleared |
Timeout holds a reference to state after unmounting. |
LeakyInterval.tsx |
setInterval not cleared |
Interval continues to update state after unmounting. |
LeakyListener.tsx |
Event listener not removed | resize listener retains a reference to the component state. |
Component | Purpose | Notes |
---|---|---|
RecipeHeader.tsx |
Displays recipe metadata | Modular and reusable. |
IngredientCard.tsx |
Renders individual ingredients | Contains rendering inefficiencies. |
InstructionStep.tsx |
Shows step-by-step instructions | Could be optimized further. |
RecipeList.tsx |
Lists all recipes | Demonstrates potential coverage issues. |
- Open Chrome DevTools.
- Navigate to the Performance tab:
- Click "Record" and interact with the app (e.g., toggling leaky components or navigating between views).
- Look for a continuously growing JS Heap size, which may indicate memory leaks.
- Use the Memory tab:
- Take a heap snapshot and analyze retained objects, closures, and detached DOM nodes.
- Open Chrome DevTools and navigate to the Coverage tab:
- Click "Start Instrument Coverage and Reload Page" to begin recording.
- Interact with the app (e.g., navigating or triggering components).
- Observe the report:
- CSS: Look for rules that are not applied during interactions.
- JavaScript: Identify functions or scripts that are loaded but never executed.
- Export the coverage report to analyze large, unused files for optimization.
- Open the Performance Tab in Chrome DevTools:
- Click "Record" and perform app interactions (e.g., scrolling, clicking, or navigating).
- Analyze the timeline:
- Look for long tasks or repeated layout shifts.
- Identify components with slow rendering or excessive re-renders.
- Use Flame Charts:
- Pinpoint functions or components consuming the most time.
- Open the Network Tab in Chrome DevTools:
- Reload the app and monitor network requests.
- Analyze API calls:
- Check for failed or unnecessary requests.
- Monitor response times and payload sizes.
- Optimize resource loading:
- Identify large or unoptimized assets (e.g., images, JavaScript bundles).
- Ensure HTTP caching is utilized for static files.
- Use Vite Bundler Analyzer (
pnpm analyze
):- Visualize the size of individual modules in your bundle.
- Identify oversized dependencies or unused packages.
- Use Bundlephobia:
- Analyze npm packages to detect size, treeshaking support, and dependency impact.
- Replace heavy or unused packages with lighter alternatives if possible.
- Run Lighthouse in Chrome DevTools:
- Analyze metrics such as Largest Contentful Paint (LCP), Cumulative Layout Shift (CLS), and Total Blocking Time (TBT).
- Review optimization recommendations, such as eliminating render-blocking resources or optimizing images.
- Use WebPageTest:
- Evaluate real-world page loading speeds and resource loading patterns.
- Compare results across multiple geographic locations for better insights.
Issue | Solution |
---|---|
Uncleared Timers | Use clearTimeout or clearInterval in useEffect cleanup functions. |
Unremoved Listeners | Use removeEventListener in useEffect cleanup functions. |
Unused Code | Remove unused CSS and dead JavaScript code. |
Inefficient Rendering | Use React.memo , useCallback , and useMemo to optimize rendering. |
- Chrome DevTools:
- Lighthouse Tab: Evaluate app performance and discover optimization suggestions.
- Performance Tab: Analyze memory leaks and heap usage.
- Coverage Tab: Detect unused code.
- Network Tab: Inspect API and resource performance.
- Source Tab: Review the source code and debug issues.
Here’s an example of how to fix a memory leak in LeakyTimer.tsx
:
useEffect(() => {
const timeoutId: NodeJS.Timeout = setTimeout(() => {
setMessage("Timeout Finished!");
}, 5000);
return () => {
clearTimeout(timeoutId); // Cleanup to prevent leaks
};
}, []);
This application is a demonstration of common issues:
- Memory leaks
- Inefficient rendering
- Unused code
It serves as a learning tool to better understand how to identify, debug, and fix these issues in real-world applications.
Feel free to fork the repo and experiment further!