blog content
The following article is a part of The Ultimate Guide to React Native Optimization and describes the impact of using dedicated libraries for mobile.
Why is it important?
React Native gives you total control and freedom in choosing the way you build your apps. It allows you to write the code in JavaScript, reuse your current React components and use various libraries. But what you need to remember is that not every library is optimized for mobile. Using non-optimized libraries may cause some serious performance and user experience issues that can negatively impact the business side of your project. In this article, we will tell you how to avoid such problems and choose the right libraries for your apps.
In other blog posts based on The Ultimate Guide to React Native Optimization, we touch on the following topics related to improving performance through understanding the React Native implementation details:
- Picking the right external libraries for your app
- Finding the balance between native and JavaScript
- Animating at 60FPS
- Improving React Native performance with high-order components
- Optimizing your React Native app’s JavaScript bundle
Be sure to check them out. Now, let’s dive into the best practices for choosing the libraries dedicated to mobile.
Why web libraries do your mobile app no good
One of the best things about React Native is that you can write the mobile application with JavaScript, reusing some of your React components and doing the business logic with your favorite state management library.
While React Native provides web-like functionality for compatibility with the web, it is important to understand that it is not the same environment. It has its own set of best practices, quick wins, and constraints.
For example, while working on a web application, we don’t have to worry too much about the overall CPU resources needed by our application. After all, most websites run on devices that are either plugged to the network or have large batteries.
It is not hard to imagine that mobile is different. There’s a wide range of different devices with different architectures and resources available. Most of the time, they run on battery and the drain caused by the application can be a deciding factor for many developers.
In other words, how you optimize the battery consumption both in the foreground and background can make all the difference.
Not using mobile-optimized libraries cause battery drain and slows down your app
While React Native makes it possible to run the same JavaScript on mobile as in the browser, that doesn’t mean you should be doing this every time. As with every rule, there are exceptions.
If the library depends heavily on networking, such as real-time messaging or offers an ability to render advanced graphics (3D structures, diagrams), it is very likely that you’re better going with the dedicated mobile library.
Mobile libraries were developed within the web environment in the first place, assuming the capabilities and constraints of the browser. It is very likely that the result of using a web version of a popular SDK will result in extraneous CPU and memory consumption.
Certain OSes, such as iOS, are known to be constantly analyzing the resources consumed by the application to optimize the battery life. If your application is registered to perform background activities and these activities take too much of the resources, the interval for your application may get adjusted, lowering the frequency of the background updates that you initially signed up for.
Use a dedicated, platform-specific version of the library to optimize your React Native app
Let’s take Firebase as an example. Firebase is a mobile platform from Google that lets you build your apps faster. It is a collection of tools and libraries that enable certain features instantly within your app.
Firebase contains SDKs for the web and mobile - iOS and Android respectively. Each SDK contains support for Realtime Database.
Thanks to React Native, you can run the web version of it without major problems:
An example reading from Firebase Realtime Database in RN
However, this is not what you should be doing. While the above example works without issues, it does not offer the same performance as the mobile equivalent. The SDK itself also contains fewer features. No surprises here; web is different and there’s no reason Firebase.js should provide support for mobile features.
In this particular example, it is better to use a dedicated Firebase library that provides a thin layer on top of dedicated native SDKs and offers the same performance and stability as any other native application out there.
Here’s how the above example would look like:
An example reading from Firebase Realtime Database in RN
As you can see, the difference is minimal and boils down to a different import statement. In this case, the library authors did a great job mimicking the API to reduce the potential confusion while switching back and forth between the web and mobile context.
Pick libraries carefully to provide the most performant support with no harm to the battery life
React Native is all about giving you control and freedom to choose how you want to build your application. For simple things and maximum reusability, you can choose to go with the web version of the library. That will give you access to the same features as in the browser at relatively low effort.
For advanced use cases, you can easily extend React Native with a native functionality and talk directly to the mobile SDKs. Such escape hatch is what makes React Native extremely versatile and enterprise-ready.
It enables you to build features faster on many platforms at once, without compromising on the performance and user experience - something other hybrid frameworks made a standard tradeoff.
Need help with performance? Give us a shout!
If you’re struggling with improving your app performance, get in touch with us.
We’re the official Meta and Vercel partners and active contributors to the community. We’ve delivered high-quality solutions for dozens of international clients, from startups to enterprises. If you have any React Native development needs, we’ve got you covered.