Is react-native javascript runner that slow?

37 Views Asked by At

I'm relatively new to React Native, having initially encountered it about five years ago, but I've recently shifted my focus to web development. I'm currently working with Expo version 50+.

I'm dealing with a resource-intensive image segmentation algorithm that makes use of flood fill and extensive arrays in its implementation. Unfortunately, I'm struggling to address a significant problem: there is a substantial increase in processing time when running the algorithm on a built APK or Expo Go on iOS.

Interestingly, when I use Expo Web, I do not experience these issues. My algorithm typically takes around 13 seconds to process an image of size 1024x1024, whether I'm running it from my PC's browser or my phone's browser. However, when I compile the app or use Expo Go, the processing time skyrockets to over 150 seconds, which not only impacts performance but also puts a significant strain on the phone's resources. Is it possible that the React Native JS runner is inherently less efficient, prompting me to consider rewriting this algorithm as a native module? However, this presents a challenge for me since I lack experience with Kotlin, Swift, and managing algorithm complexity. Do you have any potential solutions to suggest?

I considered the option of running this algorithm on a server, but unfortunately, even when using AWS Lambda, it takes 60 seconds to process, which doesn't meet my requirements. Additionally, I anticipate that running this algorithm on a scalable EC2 instance could be prohibitively expensive.

While I have made efforts to optimize my algorithm, it's perplexing to see such a drastic increase in processing time, going from 13 seconds to over 150 seconds when using Expo Go or a built APK.

1

There are 1 best solutions below

2
Zenb0t On

A suggestion would be running the new architecture: docs, which supports multi-threading. Another option would be to use a lib like react-native-reanimated and use the UI threat to help compute your algorithm. I'm sure there are other ways, maybe using Skia, to speed things up. Since you will have considerably fewer resources in a phone, you might want to think about your algorithm and implement it using something different than JS, if you need to run it elsewhere (like an EC2).

Without seeing the algorithm complexity, and understanding what you are doing, it is hard to point you towards a definitive solution.

Lastly, I would check if you are building your APKs for production or development. This would be a game changer on performance, if it is the case.