r/reactnative • • 5h ago

Code Splitting - Lazy Evaluation in React Native

I have built a bare react native cli toy project to study how lazy evaluation works and how it effects app cold start and TTI.

This is the nested navigation architecture of the app. I added some expensive computations in inner screens just to test the lazy evaluation.

App

└── RootNavigator (native stack)

├── MainTabs (bottom tabs, lazy={true})

│ ├── HomeTab → HomeStack

│ │ ├── Home

│ │ └── HomeDetail

│ ├── ProfileTab → ProfileStack

│ │ ├── Profile

│ │ ├── ProfileSettings → chartData

│ │ └── AccountSecurity → securityCrypto

│ └── AnalyticsTab → AnalyticsStack

│ ├── AnalyticsDashboard → heavyMath (+ 50M local loop)

│ ├── Reports → reportEngine

│ └── DeepInsights → insightsEngine + chartData

└── AppSettings (modal)

referred the following guide to implement this:

https://reactnative.dev/docs/optimizing-javascript-loading

Problems:
-> while the TTI and cold start times increased drastically for initial screens, lazy evaluation of inner screens is being effected.
Eg: when we click on a button navigating to an inner screen that has a lazily evaluated js code, the button stays unresponsive for a few seconds before navigating. Also the TTI of the screens has also increased.

I tried preloading the screens which are immediate siblings but while their code is being evaluated the currently active screen stays unresponsive.

Technique used: Inline requires, Lazy Loading, Hermes engine

Expensive computations employed in screens:
I know these computations are an overkill but its just to test.

I would appreciate any suggestions, insights or any knowledge you could share on this.

ModuleWorkScale
AnalyticsScreen (inline)Sum 0…n50M iterations
heavyMathRolling checksum (checksum + i*31) % 1e9+740M
reportEngineAggregate + sample rows35M
securityCryptoFNV-style hash over seed30M
chartDataArea + sparse points28M
insightsEngine1200×120 matrix + extra burn25M
2 Upvotes

2 comments sorted by

1

u/atta542 5h ago

that hang is kind of the expected tradeoff, you didn't remove the work, you just moved it to the moment of the tap. the 50M loop is synchronous on the js thread so nothing can render until it finishes, lazy or not. a couple things that might help: kick off the require for the next likely screen once the first screen is interactive (requestIdleCallback or a small setTimeout), so the evaluation happens while the user is still looking around. and for the heavy computation itself, render the screen first with a loading state and run the work after mount, or chunk it so the thread gets breaks. if it's real cpu heavy work in a real app, that's where a worklet or a native module starts to make sense