firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

enablePersistence slow(1000ms) during app init.

Open InputNeuron opened this issue 7 years ago • 16 comments

[REQUIRED] Describe your environment

  • Operating System version: Ubuntu 18.04
  • Browser version: Google Chrome | 70.0.3538.77 (Official Build) (64-bit)
  • Firebase SDK version: 5.8.6
  • Firebase Product: firestore

Calls to firebase.firestore().enablePersistence({ experimentalTabSynchronization: true })

are slow...

Taking up to 1000ms on my browser. Seems like a huge time sync for my app as I want to load the entire page in < 500ms max.

InputNeuron avatar Apr 13 '19 20:04 InputNeuron

Sorry to hear about the issue. I'll have to dig into this to find out what's causing the slowness. In the meantime, can you please provide some additional info?

  • is 1000ms the average value you see? If not, what is the average value?
  • does removing experimentalTabSynchronization improve things at all?
  • do you happen to have tabs with other instances of Firestore open when you're measuring speed?

var-const avatar Apr 15 '19 22:04 var-const

i can confirm this behaviour and i think its related to indexedDB size.. bigger size more delay. but generally without enablePersistence much faster (500 - 2000 ms)

mohshraim avatar Apr 15 '19 22:04 mohshraim

@burtonator Thanks for contacting us! To help us out, can you provide a little more information?

If you are able, can you look at the clientMetadata object store in the Developer Tools and let us know how many entries you see?

objectstore

While enabledPersistence() can theoretically perform other tasks as well (since it is the first function call and invokes client initialization), the time it takes to start the client might depend on how many items it has to read from the clientMetadata object store.

Please also note that we run schema migrations when you launch the client with enablePersistence() for the first time. When you are measuring the startup time, it might make sense to exclude the first run.

schmidt-sebastian avatar Apr 16 '19 23:04 schmidt-sebastian

Hey @burtonator. We need more information to resolve this issue but there hasn't been an update in 7 days. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Apr 24 '19 01:04 google-oss-bot

Hey @schmidt-sebastian and @var-const , I've run into the same issue, so am happy to help add more input. I'm currently just calling enablePersistence without waiting for the result of the promise before continuing and loading everything from firestore (it seems to work, but I'm not relying on this for a production app, so don't worry about me relying on this 😃).

My code looks like this (waiting for the promise to resolve):

console.time('enablePersistence');
firebase
    .firestore()
    .enablePersistence({ experimentalTabSynchronization: true })
    .then(() => console.timeEnd('enablePersistence'))

To answer the first set of questions from @var-const :

  • I see times roughly between 1000 and 1200ms (I took about 20 samples). Note: I am in Australia and I'm using firestore in us-central, so would expect a bit of latency if a network request needs to be made, but only ~200ms. I can't remember exactly why I picked us-central, IIRC it was to do with cloud functions not being available there, or similar.
  • Removing experimentalTabSynchronization doesn't seem to have any effect on the time.
  • I'm only opening a single tab of my app.

@schmidt-sebastian for your question - I looked at the clientMetadata object as you described, after doing the following: clearing site data (using "clear storage" on the application tab), refreshing the page and confirming I'm still getting 1000-1200ms times for enablePersistence. There's only one entry there.

oli-w avatar Apr 26 '19 09:04 oli-w

@oli-w Thanks for the update. This will hopefully help us isolate the root cause.

As a quick note, we designed enablePersistence() so that you don't have to wait for the returned Promise to resolve before issuing any other Firestore operations.

schmidt-sebastian avatar Apr 26 '19 16:04 schmidt-sebastian

As a quick note, we designed enablePersistence() so that you don't have to wait for the returned Promise to resolve before issuing any other Firestore operations.

OH!!! That seems like a huge and awesome revelation!

Thanks! What's the downside if I don't await it? What happens?

@oli-w > clearing site data (using "clear storage" on the application tab), refreshing the page and confirming I'm still getting 1000-1200ms times for enablePersistence. There's only one entry there.

... you might also want to just look at localStorage in the console just to make sure.

Pretty amazing that it's still 1000ms even without storage assigned.

Excited to get this resolved as my app will be a LOT faster!

InputNeuron avatar Apr 26 '19 19:04 InputNeuron

As a quick note, we designed enablePersistence() so that you don't have to wait for the returned Promise to resolve before issuing any other Firestore operations.

As @burtonator also commented, I think this is actually really key. An update to the docs would help a lot - that you don't need to wait for the result of the promise before making calls to get data from firestore. The docs for the method says:

Must be called before any other methods (other than settings()).

but doesn't clarify if you need to just call enablePersistence before calling other methods, or if you need to also wait for the promise the complete (resolve or reject).

Overall I'm loving firebase, please keep up the great work!

Excited to get this resolved as my app will be a LOT faster!

@burtonator - sounds like your app will already be a lot faster (without having to address this issue) if you just proceed with execution in your app without waiting for the resolve / reject?

oli-w avatar Apr 28 '19 09:04 oli-w

As a quick note, we designed enablePersistence() so that you don't have to wait for the returned Promise to resolve before issuing any other Firestore operations.

OH!!! That seems like a huge and awesome revelation!

Thanks! What's the downside if I don't await it? What happens?

Firestore executes all internal logic all user-operations that return Promises sequentially. If you call enablePersistence() followed by await documentReference.get(), the delay during the enablePersistence() call will delay resolving the Promise returned from get(). There are no behavior changes assuming that the call to enablePersistence() eventually succeeds.

You need to wait for the Promise if it matters to you whether the call fails (e.g. the browser's IndexedDB implementation is not supported, you are using multi-tabs without experimentalTabSynchronization).

We can certainly update our docs to state that you don't need to wait for enablePersistence() to resolve. It is only important that you invoke the method before any other operation.

schmidt-sebastian avatar Apr 28 '19 14:04 schmidt-sebastian

@schmidt-sebastian Thanks! This is amazing. Seriously improved the performance of our app. Feels MUCH MUCH faster now!

We're not awaiting it and blocking but we're catching any error and going to show it directly to the user if a problem happens.

InputNeuron avatar Apr 29 '19 15:04 InputNeuron

Ah.. spoke too soon. Migrating this to just a catch() doesn't raise an issue but our auth handler doesn't work. This might be a bug but I will spend more time with it.

Either app.app.auth() is null or auth.currentUser is null.

I'm also only a slightly older firebase but not too old. I'll try to upgrade there too.

InputNeuron avatar Apr 29 '19 15:04 InputNeuron

Tried with Firebase 5.10.1 right now and the same issue.

auth.currentUser is null but maybe I need to await the auth metadata...

InputNeuron avatar Apr 29 '19 15:04 InputNeuron

Internally Firestore waits for auth to give its first credential changed event before enabling persistence, so unless you specifically need the auth metadata for your own purposes, you shouldn't need to worry about that. See FirestoreClient.start in the source for more details.

wilhuff avatar Apr 29 '19 17:04 wilhuff

@burtonator, we're unable to reproduce the slowness you're describing. My worry is that while your 200ms RTT expectation is reasonable for a single round trip, there may be more than one round trip required.

If you enable network tracing in chrome dev tools, what do the timings look like for requests to firebase backends?

wilhuff avatar Apr 29 '19 21:04 wilhuff

Hey @burtonator. We need more information to resolve this issue but there hasn't been an update in 7 days. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar May 07 '19 01:05 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@burtonator if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar May 10 '19 01:05 google-oss-bot