42+ ways to make your React app faster ⚛️:
Performance Testing
Generate a simple fake dataset in a loop.
export function generateProducts() {
const products = [];
for (let i = 0; i < 10000; i++) {
products.push(`Product ${i+1}`);
}
return products;
}
Generate a large fake dataset using tools like Faker, Chance, etc.
import faker from "faker";
export const fakeNames = Array.from(Array(10000), () => {
return faker.name.findName();
});
Simulate a slow component via a loop:
function ExpensiveTree() {
let now = performance.now();
while (performance.now() - now < 100) {
// Artificial delay -- do nothing for 100ms
}
return <p>I am a very slow component tree.</p>;
}
Forms
HTTP
Rendering
Context
Routing
Keys
State
Memoization / Identity
Props
Component composition / Children
Design
Bundle optimization
Third party libraries
Styling
Framework
More examples
https://piyushsinha.tech/optimizing-performance-in-react-apps-i
https://piyushsinha.tech/optimizing-performance-in-react-apps-ii
42+ ways to make your React app faster ⚛️:
Performance Testing
console.timelike this, against the prod build, to measure slowness and compare to a before and after to determine ifuseMemois a net win:console.time('filter array'); const visibleTodos = getFilteredTodos(todos, filter); console.timeEnd('filter array');Generate a simple fake dataset in a loop.
Generate a large fake dataset using tools like Faker, Chance, etc.
Simulate a slow component via a loop:
Forms
HTTP
Rendering
useEffect). React-query's prefetching is a simple way to render as you fetch. Remix also does this by default since nested routes declare data dependencies and run in parallel on the server. My tweet on thisContext
Routing
Keys
keyfor dynamic lists with stateless items, where items are replaced with the new ones - paginated lists, search and autocomplete results and the like. This will improve the list’s performance because the entire thing doesn't have to re-render. The component instances are reused. Demo and blog postState
useTransitionfor low priority updates (reduces jank) - DemouseLayoutEffectto avoid a Flash of unstyled content when you need to read and manipulate the DOM before the user sees it - DemoMemoization / Identity
useMemoanduseCallbackProps
useTransitionbecause you don't control the value coming in (coming from third party or via a prop you can't control). Another demoComponent composition / Children
childrendon’t re-render since they are just props. Note: don't pass a function as a child, since it'll still re-render because the func will be recreated on each renderDesign
Bundle optimization
Third party libraries
import { Tab } from "x". Preferimport Tab from "x/Tab"when possible.Styling
Framework
More examples
https://piyushsinha.tech/optimizing-performance-in-react-apps-i
https://piyushsinha.tech/optimizing-performance-in-react-apps-ii