December 4, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

A Guide To Eliminate Common Performance Issues in React Native App Development

Great post from our friends at Source link

“The more the navigation controls, animations, and third-party libraries, the more it affects performance.”

React native app development does have performance issues. The guide determines solutions for common performance issues in React Native, focusing on memory leaks, size, navigation, etc., in apps. Both learners and professional programmers can inform themselves with the tricks provided or take help from expert app development companies.

React native is a promising framework in providing cross-platform development for applications. The framework is a JavaScript library providing the native feel to the application. It is built by a renowned company, Meta (Facebook), and has combined the best parts of React programming language with JavaScript library. React Native came up with new features and breaking changes with its new version 0.67 in 2022.

Since the framework is rich in new features it still lacks solutions to the performance issues. The guide below is for beginners as well as professional programmers who want to learn or recall eliminating common performance issues in React Native app development.

Although a few issues are still in development from the React Native team, we have tried to cover many major performance issues and enlisted their solutions.

Let us start with issues on the Android operating system.

Performance Issues in Android

Android is an experimental operating system because of its open-source. The libraries are free to download, and anyone can step into android development and apply React Native framework. React Native and Android have a few performance issues that developers can eliminate with basic alterations and logic.

Memory Leak in Apps

Causes

  • Apps slow down when users load many threads or activities simultaneously, causing the app to crash or be unresponsive.
  • For example, in a music app, a user keeps loading new songs through scrolling. Until they reach page 3, the app starts causing performance issues due to memory leaks (memory not being returned to the operating system).

Solutions

  • Instead of using ListView, it is good to use FlatList or VirtualizedList. Below is a snippet of how FlatList is applied.
<FlatList
      data={elements}
  keyExtractor={item => `${items.name}`}
      renderItem={({ item }) => <Item key={item.name.toString()} />}
/>

  • With FlatList in React Native, users can perform an infinite scrolling experience because it quickly refreshes items, rendering a smooth performance.
  • SectionList is also a good idea to use to eliminate memory leaks.
  • Finally, PerfMonitor is an inbuilt performance monitor provided by React Native to monitor the app’s performance and read memory leaks. Perfmonitor can be imported by importing the below library.
Import PerfMonitor from 
'react-native/Libraries/Performance/RCTRenderingPerf';

Size of the Application Affecting Performance

Causes

  • The application connects with third-party sources a number of times showing information on a number of screens which ultimately increases the application size overloading the memory.
  • Also, an app rich in features with multiple threads working at once affects app size and, ultimately, the application’s overall performance.

Solutions

  • ProGuard is a good way to decrease the file size of APK since it strips the parts of binaries.
  • It is also good to make specific APK files for the phone’s architecture, removing the requirement of JSCore binary files that support a variety of phone architectures.
  • Images and other graphical content take more size, so it is good to compress the graphic files. For example, .png files are more in size, so APNG can be used with a smaller size, ultimately decreasing the size of the application.
  • Organized libraries also take less size, so one can opt for the organization of the libraries, like minimizing the resources after use.
  • Not storing the JSON data reduces app size by converting them into static object IDs.

Navigation in JS and React

Causes

  • Javascript and Native threats are challenging to bridge sometimes because of the inconsistencies in JS Navigators.
  • Navigator is a pure JavaScript component widely used for navigation. It is not good in performance, and one has to write different codes of bridging for IOS and Android.

Solutions

  • React navigation is the choice for Native since it works efficiently with redux and Javascript providing better satisfaction in the app performance.
  • It uses routers for navigation logic while understanding it with Redux.

Faster Rendering of Views

Causes

  • While batching unoptimized JS rendering components with different algorithms, one returns with non-optimization.

Solution

  • Using virtual DOM with syncs with the real DOM provides instant UI changes to the users.
  • It batches JS rendering components with a different algorithm. The minimized data is sent over the bridge, improving the syncing process as used by big apps like Uber eats to boost their performance and UI changes.

Delayed Updates

Causes

  • React native communicates with Javascript thread to offload the complex functions.
  • The process causes a delay in the update, ultimately degrading the app’s overall performance.

Solution

  • We do not have a fixed solution for this, and the React Native team is working to improve the activities.

Improved Launch Time in the App

Causes

  • Some aspects of the app cause launching and performance issues.

Solution

  • Object.finalize element can be used to improve the app launch.
  • It is essential to strategically plan the threads since finalizers run on a single thread. Until one threat is garbage collected, the other one doesn’t launch.

Image Caching

Causes

  • Libraries like NPM store images on the file system as prescribed in general cache implementation logic.
  • Whenever the app refreshes, the cache is missed, and libraries ultimately fail in fetching data.

Solution

  • Using React native cacheable image library can help in cache failure. Below is a snippet to use the cached image.
import { CachedImage } from 'react-native-cached-image';

<CachedImage 
  style={{ ... }}
  source={{ uri: item.urls.raw }}
/>

Performance Issues in iOS

Apple provides a stable ecosystem. Despite being stable, the iOS operating system still has performance issues while working with React Native.

Size Adjustment With Original Size

Causes

  • While adjusting the height and width, the scaling is a crop of the original image, so the size remains the same, consuming the same amount of memory.

Solution

  • One can use [{scale}] property to fit image sizes for different UI views so that one doesn’t have to crop it.
  • Custom FadeInIMage can be used for better memory efficiency so that the OnLoad element can project the images quickly, improving the overall performance.

Multithreading Issues

Causes

  • React Native doesn’t support multiple threads at the same time.
  • One component has to wait until the other is loaded.

Solution

  • It is a good idea to write extension code in other languages that support multithreading.
  • Languages can be java, objective c, swift.

Infinite Scrolling Optimization

Causes

  • In iOS, ScrollView and ListView create issues in the performance.

Solution

  • Window-based sliding is an excellent option to eliminate the performance issues while scrolling.
  • The position of elements gone up remains fixed but changes the sliding window preventing overload of the memory in one slider.

Image Loading in iOS

Cause

  • Sometimes due to slow internet connection, images do not load, and the image grid looks empty without content.

Solution

  • Using a good thumbnail in the placeholder is a good option. An example of a placeholder is shown below:
<ProgressiveImage source={{uri: stories.imageurl}} thumbnail={require("../../images/placeholder.png")} style={{width:viewportWidth,height:200}} key={"pimg"}/>

Conclusion

Apps are becoming heavy in size with the added functionalities and features. ReactNative is an excellent choice to develop apps, but it still has common performance issues. I hope this guide helps you develop robust solutions to eliminate common performance issues in React Native.

#Guide #Eliminate #Common #Performance #Issues #React #Native #App #Development