Skip to content

Optimize Bar component rendering when activeBar is falsy#6290

Merged
ckifer merged 1 commit intomainfrom
bar-optimize
Sep 8, 2025
Merged

Optimize Bar component rendering when activeBar is falsy#6290
ckifer merged 1 commit intomainfrom
bar-optimize

Conversation

@PavelVanecek
Copy link
Collaborator

@PavelVanecek PavelVanecek commented Sep 8, 2025

Description

Do not read which index is active if we know we will never use this information anyway.

This optimizes the chart when activeBar is falsy. It remains slow when activeBar is set! But it is the same slow in 2.x.

Related Issue

Fixes #6267

How Has This Been Tested?

Exhibit A, before any changes

Screenshot 2025-09-08 at 20 39 57

Mouse hover - 177m jank

Lot of time spent in rendering Rectangle - updateFunctionComponent 41% (256ms)

Chart takes about 4 seconds to load before it's interactive.

Exhibit B: with optimized animation ID dependencies

Screenshot 2025-09-08 at 20 45 03

Only generate new animation IDs when props that actually animate change.

Mouse hover - 50-110ms jank

updateFunctionComponent still high 52% (539ms)

Feels about the same in the browser, no subjective improvement.

4.5s to interactive.

Exhibit C: with hooks extracted to child component

Screenshot 2025-09-08 at 20 49 29

introduce BarRectangleWithActiveState that calls the selectActiveTooltipIndex selector in a child, not in a loop.

Mouse hover - 50-110ms jank

updateFunctionComponent still high - 46% 416ms

This subjectively feels faster but profile says it's about the same.

3.9s to interactive.

Exhibit D: do not call hooks if active prop is not set

Screenshot 2025-09-08 at 21 28 27 Screenshot 2025-09-08 at 20 53 30

Adds BarRectangleNeverActive so that we never call any hooks if activeBar prop is falsy.

Looks like a jackpot, the Tooltip is as smooth as it gets on my 60fps screen.

Mouse hover - no jank

updateFunctionComponent Rectangle went away - it never updates at all. The Tooltip is still there, that has to stay.

3.8s to interactive, this didn't get any better.

Okay I'm happy with this. The chart remains slow with activeBar prop but that's the same in 2.x so it's not a regression anymore.

Copilot AI review requested due to automatic review settings September 8, 2025 12:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the Bar component rendering when the activeBar prop is falsy by conditionally using hooks only when necessary. The optimization prevents unnecessary selector calls and React reconciliation when bars don't need active state tracking.

  • Extracts Bar rectangle rendering into separate components with and without active state hooks
  • Optimizes animation ID generation by memoizing only relevant properties
  • Refactors SVG properties filtering to use a reusable type alias

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/util/svgPropertiesNoEvents.ts Adds type alias for cleaner return type definition
src/shape/Rectangle.tsx Optimizes animation ID generation by memoizing only animation-relevant properties
src/cartesian/Bar.tsx Splits bar rectangle rendering into conditional components to avoid unnecessary hook calls when activeBar is falsy

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@codecov
Copy link

codecov bot commented Sep 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.63%. Comparing base (44c78d3) to head (129fbd4).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6290   +/-   ##
=======================================
  Coverage   96.63%   96.63%           
=======================================
  Files         221      221           
  Lines       20195    20205   +10     
  Branches     4145     4150    +5     
=======================================
+ Hits        19515    19525   +10     
  Misses        673      673           
  Partials        7        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov
Copy link

codecov bot commented Sep 8, 2025

Bundle Report

Changes will increase total bundle size by 3.46kB (0.14%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.05MB 1.53kB (0.15%) ⬆️
recharts/bundle-es6 900.66kB 1.51kB (0.17%) ⬆️
recharts/bundle-umd 488.2kB 425 bytes (0.09%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
cartesian/Bar.js 1.39kB 25.33kB 5.78% ⚠️
shape/Rectangle.js 142 bytes 9.39kB 1.53%
util/svgPropertiesNoEvents.js -1 bytes 5.24kB -0.02%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
cartesian/Bar.js 1.38kB 23.85kB 6.12% ⚠️
shape/Rectangle.js 139 bytes 8.37kB 1.69%
util/svgPropertiesNoEvents.js -1 bytes 5.07kB -0.02%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 425 bytes 488.2kB 0.09%

@PavelVanecek
Copy link
Collaborator Author

PavelVanecek commented Sep 8, 2025

Perhaps we could use https://react.dev/reference/react/memo for the activeBar case, let me try

Copy link
Member

@ckifer ckifer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🚀

@ckifer
Copy link
Member

ckifer commented Sep 8, 2025

will merge this as is

@ckifer ckifer merged commit b7d164e into main Sep 8, 2025
27 checks passed
@ckifer ckifer deleted the bar-optimize branch September 8, 2025 15:50
@patrikholcak
Copy link

Legend

@PavelVanecek PavelVanecek mentioned this pull request Nov 15, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance degradation for large datasets using bar charts - v3.0

4 participants