React setState takes 200ms
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When setState appears to take 200 milliseconds, the problem is usually not the state setter itself. The real cost is almost always in the render work triggered by the update: expensive calculations, too many component re-renders, heavy DOM work, or large lists updating at once. To fix it, profile the update path instead of assuming the setter is inherently slow.
setState Schedules Work
In React, setState or a state setter from useState schedules an update. React then decides what needs to re-render and commits the necessary DOM changes.
The setter call itself is tiny. What becomes expensive is everything that happens after it.
Look for Expensive Rendering
A common issue is recomputing large derived values on every render.
If items is large, a state update elsewhere can make this component expensive. Use memoization when the computation is stable relative to its inputs.
Prevent Unnecessary Re-renders
If one small state change causes a large subtree to re-render, split components and memoize where appropriate.
If props do not change, memo can stop wasted work.
Profile Before Guessing
Use the React DevTools Profiler to see which components re-render and how long they take. That tells you whether the cost comes from reconciliation, large lists, layout thrashing, or third-party components.
Without profiling, developers often optimize the wrong layer.
Production Builds Matter
Always compare performance in a production build before drawing conclusions. Development mode includes extra warnings and validation work, and React Strict Mode may intentionally trigger additional render paths during development. A slow development build does not automatically mean the shipped application has the same problem.
If the lag still appears in production, then the profiler results are much more likely to reflect a real user-facing bottleneck.
That is the point where memoization, virtualization, or state-ownership changes usually become worth the complexity.
Until then, measure first and optimize second.
That avoids cargo-cult tuning.
Common Pitfalls
- Blaming
setStateinstead of the render and commit work that follows. - Performing expensive filtering, sorting, or mapping on every render.
- Updating large lists without virtualization.
- Passing new object and function props everywhere and defeating memoization.
- Measuring performance in development mode only, where React does extra checks.
Summary
- '
setStateitself is usually not the slow part.' - Slow updates usually come from expensive renders or DOM work after the state change.
- Use memoization, component splitting, and list virtualization where appropriate.
- Profile with React DevTools before optimizing.
- Optimize the update path, not just the setter call.
Related reading
- Read capacity cost of a DynamoDB table scan
- read line by line in the most efficient way platform specific
- Rearrange a list of points to reach the shortest distance between them
- Reasonable optimized chart scaling
- React, setState with async updater parameter?
- React/RCTBridgeModule.h file not found
- Rebalancing rate when new node is added
- recalculate ray tracing/casting costs when changing size of rectangle

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.