Expensify / react-native-onyx

Persistent, offline-first state management solution for React Native. Easy to use with minimal config and boilerplate.
MIT License
160 stars 73 forks source link

Improve `useOnyx` performance #551

Closed fabioh8010 closed 4 months ago

fabioh8010 commented 5 months ago

Details

@hurali97

This PR improves useOnyx performance by introducing two changes:

  1. Optimises getSnapshot() function to only get the newest cached value from Onyx IF we are connecting to Onyx.connect() for the first time OR after the Onyx.connect() callback is fired, since getSnapshot() can be called several times by React and we only want to update the cache under this specific condition.
  2. Optimises previous cached value / new cached value comparison to only use deepEquals IF key is a collection key AND selector is defined. shallowEquals is enough for the other cases and will be used instead.

Related Issues

https://github.com/Expensify/App/issues/42421

Automated Tests

Manual Tests

Some common tasks on E/App:

  1. Start a new chat and search for someone (uses useOnyx).
  2. Send a message to someone.
  3. Open someone's profile and change settings (uses useOnyx).
  4. Open Preferences and change settings (uses useOnyx).

Author Checklist

Screenshots/Videos

Android: Native https://github.com/Expensify/react-native-onyx/assets/20051562/2f95b10b-82cd-444f-9c3e-5601b3f7b1e8
Android: mWeb Chrome https://github.com/Expensify/react-native-onyx/assets/20051562/7e9dc885-f706-4965-9673-e1bc915e7b35
iOS: Native https://github.com/Expensify/react-native-onyx/assets/20051562/08b8e24d-978e-4d15-b49b-23ee1f1302f7
iOS: mWeb Safari https://github.com/Expensify/react-native-onyx/assets/20051562/41fbe307-c83f-4d61-abee-69cdab222364
MacOS: Chrome / Safari https://github.com/Expensify/react-native-onyx/assets/20051562/dbb60761-598b-4f44-87ac-607897bbff65 https://github.com/Expensify/react-native-onyx/assets/20051562/6caecf22-2d74-400a-9407-0f4eaf2ceab2
MacOS: Desktop https://github.com/Expensify/react-native-onyx/assets/20051562/8ee94825-4bf7-4c2c-84df-6f19d8416cbc
fabioh8010 commented 5 months ago

@janicduplessis Sure, all the details about this solution can be found here.

fabioh8010 commented 5 months ago

@mountiny This PR is still waiting for the Onyx bump, right?

mountiny commented 5 months ago

Correct

github-actions[bot] commented 4 months ago

🚀Published to npm in v2.0.52