r/reactjs • • Nov 03 '25

Discussion Facebook.com has 140 layers of context providers

I opened up React Devtools and counted how many layers of React Context Providers each social media app had, here are the results:

  1. Facebook – 140

  2. Bluesky – 125

  3. Pinterest - 116

  4. Instagram – 99

  5. Threads – 87

  6. X – 43

  7. Quora – 28

  8. TikTok – 24

Note: These are the number of <Context.Provider>s that wraps the feed on web, inspected using React DevTools.

- The top 3 have over a ONE HUNDRED layers of context!
- Many of them are granular – user / account / sharing, which makes sense, because you want to minimize re-renders when the values change
- Many only have a few values in them, some contain just a boolean

Context usage is not inherently bad, but having such a deep React tree makes things harder to debug. It just goes to show how complex these websites can be, there are so many layers of complexity that we don't see.

568 Upvotes

130 comments sorted by

View all comments

263

u/bgirard Nov 03 '25

I work on performance for Facebook.com. We have a ton of extra context because we break them down for performance reasons. We break them down to minimize rerendering when a context changes.

For instance the router context is many sub context. If we change the visibility of the page we don’t have to rerender components that are « subscribing » to other router properties like we would if we had a single router context.

With context selectors this would go away.

38

u/Full-Hyena4414 Nov 03 '25

Why is meta/react so reluctant to use context selectors?

80

u/bgirard Nov 03 '25

I can't speak for Meta.

Personally, when I saw we had context perf issues and we didn't have any better options at the time, I worked with others to decompose the context. And now perf has been good in that area so I haven't done a deep dive in that area in years. Just like anything, when it starts to show up as a hotspot in profilers, I do a deep dive and fix it. Rinse and repeat. The profiler chooses what I look it :D.

Having many contexts is perfectly acceptable for most apps, even preferred, if structured correctly.

11

u/Full-Hyena4414 Nov 03 '25

I see thx!I'd love to get more into profiler, does it point to the Context components exactly that's how you found them?

40

u/bgirard Nov 03 '25

My workflow typically looks like this:

  1. Use the Chrome Performance timeline to look for general problems.

  2. If I see too much re-rendering then I use React DevTools to see why components are re-rendering heavily.

  3. This tells me why components are re-rendering.

  4. If it's because of a context but the value we're reading from the context isn't changing then spliting that context up will typically fix it.

4

u/Full-Hyena4414 Nov 03 '25 edited Nov 03 '25

Ty. But how do you spot react re-render specifically in Chrome timeline?Last time I checked I found hard to even get names of functions and so on, a lot of them were just "anonymous"

15

u/bgirard Nov 03 '25

The Chrome timeline has a good view of CPU usage. If that's showing too much rendering component, particularly when doing an interaction that shouldn't be rendering many component, then it could be because of a re-render getting out of hand and invalidating most of the tree.

a lot of theme were just "anonymous"

Some browsers/minifier have gotten much better at giving names of anonymous functions but I'm not sure off hand where it's coming from. It's hard to say without taking a closer look. Depends on your environment, your build config, etc...

2

u/Full-Hyena4414 Nov 03 '25

Than you again

3

u/Ecksters Nov 03 '25

Are you guys using a utility to put them all together, or is it just super deeply nested?

7

u/lunacraz Nov 03 '25

oh man you're saying we shouldn't wrap everything with all providers and we should only wrap the code paths that need them? someone tell the previous engineers working on this app...

2

u/dnrvs Nov 07 '25

no it means splitting multiple values into multiple contexts. instead of `<Provider value={{ a, b }}>` you have `<Provider value={a}><Provider value={b}>`. that way subscribers to `a` don't re-render when `b` is updated and vice versa

8

u/fixrich Nov 03 '25

Check this post on the useContextSelector proposal.

Short answer is that selecting a sub-state from a larger state requires running all those selectors any time there is an update. Even if a subtree doesn't rerender, you still have to run the selector to decide if the selected value changed.

The alternative is architecting your reactive state in smaller chunks to make the idea of selecting state redundant, which is effectively what the above comment has done.

The React team seems to be exploring some middle ground where context passes a store that can be more efficient in how it handles selectors.

→ More replies (1)

7

u/NorthernElk Nov 03 '25

Hi there, I'm an FE dev that has attempted to profile my own site but often get lost in the complexity of what im looking at. It's rare to get a chance to talk to someone so experienced. Do you have any advice or resources that helped you learn your way around performance in this area?

9

u/bgirard Nov 03 '25

For me it was learning from a mix of other coworkers, writing my own DevTools and reading articles. I think soon agents will become better and will be able to capture and help you read traces. That will really benefit developers that don't have the right mentors.

5

u/[deleted] Nov 03 '25

[removed] — view removed comment

14

u/bgirard Nov 03 '25

I can't speak for the team. But many things have been explored.

It seems excessive to clutter up the dom or vdom with so many layers of pass through components

But why is it excessive? When you answer that question you end up getting data to support it or not. Someone on reddit saying that 140 is too many isn't a good answer. Conversely if I find data to show it's a performance bottleneck and would speed up the site by 5% then it's excessive. My goal is to fix other bottlenecks until this becomes a problem one day, and then I'll address it.

At the present the only hard reason I have is that it's poor DevX to have all that nesting but that's not enough to put it on top of my task queue.

→ More replies (5)

6

u/Apprehensive-Tap4653 Nov 04 '25

The facebook’s website is buggy asf or is it just me ? I just get locked on the skeletons and nothing loads for a lot of things.

→ More replies (2)

2

u/azangru Nov 03 '25

With context selectors this would go away.

When will they finally come?

5

u/acemarke Nov 03 '25

Based on recent posts and discussions from the React team, context selectors aren't going to happen because the Compiler provides 90%+ of the benefits:

They are working on a "concurrent stores" API to be a concurrent-compatible replacement for useSyncExternalStore. I've been talking with the main dev designing the concurrent stores API, who has a polyfill available, and actually just put together a proof of concept branch converting React-Redux's useSelector to use that instead:

→ More replies (1)

2

u/eduvis Nov 04 '25

Did you think about using Svelte? 😀

I know, I know. Did you think about proposing to change React's inner working to copy how Svelte works? 😀

I know I know. Do you think React is a good technology for website this complicated if performance doesn't work out of the box? Come on man, 140 layers? Really?

1

u/mr_brobot__ Nov 04 '25

So does this mean you use React.memo for the child of your context providers, so that updating one context doesn’t re-render the entire app?

I was looking into this for my Next.js app recently, only to realize that since the root layout renders a children prop, you can’t memoize it and reduce re-renders this way.

1

u/bgirard Nov 04 '25

Well React.memo or React Compiler is used where appropriate. So there's two things to consider. How many components directly depend on the context (say useContext(X)), and the potential downstream transitive render. React.memo is a tool to address the second problem if it's significant.

1

u/dimesis Nov 04 '25

Hey, why does it take 5gb in chrome to show one marketplace item? You might want to optimise that as well.

1

u/NovelAd2586 Nov 06 '25

Ever considered moving to Zustand?

→ More replies (1)
→ More replies (5)