question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve Onyx.merge performance by informing listeners that a key has changed before persisting it in local storage

See original GitHub issue

Context https://expensify.slack.com/archives/C01GTK53T8Q/p1618405920178400

Problem

When we set a key in onyx, the time it takes to inform listeners that they key was set is quite long. This is because we are waiting for the value to be saved to AsyncStorage, but there’s really no need for that wait.

Solution

We can inform listeners that the value has changed at the same time we tell AsyncStorage to save it.

Coming from https://github.com/Expensify/react-native-onyx/issues/63 the Onyx merge path is:

Onyx.merge -> get -> set -> AsyncStorage.setItem -> then(() => keyChanged(key, val))

The idea is to trigger keyChanged at the same time setItem or set is called.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
iwizniacommented, Apr 15, 2021

hmmm from those numbers, it does not look like the change is worth it, agree?

1reaction
marcaaroncommented, Apr 15, 2021

Ok so this is just one test I ran but perhaps there are some better ideas…

Test

From completely empty storage measure the time it takes for Onyx.merge() to set post downloaded reportActions data to storage and then update the subscriber of the active report. This does not take into account network request time only the time between when data is returned from the fetch() and when the component props are updated with this data.

The before is with keyChanged() called after set() resolves and the after is with keyChanged() called before set() resolves.

Web

Before: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 58

After: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 52

iOS

Before: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 63

After: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 59

Android

Before: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 558

After: Timing:expensify.cash.fetchActionsAndDataAvailableToOnyx 482

There is some measurable performance impact, but I’d be curious to see a more global test. In general, moving the call to keyChanged() before set() improved timing a bit.

Note: Android looks much slower compared to others in this test, but we are comparing Apples to Androids and not all hardware emulation is equal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improvements to `Onyx.get` · Issue #2762 · Expensify/App · GitHub
Improve Onyx.merge performance by informing listeners that a key has changed before persisting it in local storage #2397.
Read more >
LocalStorage, sessionStorage - The Modern JavaScript Tutorial
Web storage objects localStorage and sessionStorage allow to save key/value pairs in the browser. What's interesting about them is that the ...
Read more >
Where to write to localStorage in a Redux app? - Stack Overflow
I went ahead with something a little bit different: 1. save persisted state outside of redux. 2. load persisted state inside react constructor(or...
Read more >
JavaScript LocalStorage: a Complete Guide
The complete guide on LocalStorage, it's API, a code walkthrough, and various trade-offs and limitations compared to other storage options.
Read more >
Change Log for Plesk Onyx
The extension can now be installed on Plesk servers running on Red Hat Enterprise Linux 9. Plesk Migrator 2.22.0. 29 August 2022. Website...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found