React & React Native: Measuring Screen Load Time
An easy way to create custom performance monitoring for React and React Native apps
This article aims to measure the time it takes for a screen to load data and draw its UI elements. These steps must be taken before the user can interact with the UI.
Measuring load time is not just a task, it's a critical step to ensure a fast and smooth user experience. It's your key to determining if new feature development or refactoring positively or negatively impacted the app’s performance.
Get Adnan Sahinovic’s stories in your inbox
Join Medium for free to get updates from this writer.
Rest assured, after finishing this guide, you’ll build a custom hook that can be seamlessly plugged into any React, or React Native app and start measuring load time.
Implementation
Let's dive into the implementation. Our first task is to define the properties we want to use. Our goal is to create a solution that can be effortlessly integrated into any screen and start measuring time.
screenName: defined screen nameisLoading: indicating if loading is finished and a user can start with interaction
With these two in place, we can start building a custom hook.
- In
useEffectwe start measuring time withperformance.now()once the user arrives on the screen and sets the start time. - The
performance.now()method returns a high-resolution timestamp in milliseconds. - After the data is loaded, we set the
endTimeand calculate the time to load by measuring the difference between the end and start times.
Start Measuring
Let’s simply use what we have built so far and start measuring time. To do this, we need to simply import and call useGetPerformance hook with its properties.
React Native
React
Now, open a console; you should see a log message with calculated milliseconds.
We usually want to log load time to monitor it in real-time and take action. You can start logging data using the Amplitude or Datadog event logging tool.
Datadog: https://docs.datadoghq.com/service_management/events/
Amplitude: https://www.docs.developers.amplitude.com/
Happy coding!







