robinvdvleuten / vuex-persistedstate

💾 Persist and rehydrate your Vuex state between page reloads.
https://npm.im/vuex-persistedstate
MIT License
5.77k stars 378 forks source link

Debounced setState a bad idea? #406

Closed jaunt closed 3 years ago

jaunt commented 3 years ago

My app quite heavily writes to vuex at times, and since setState is calling json.stringify and writing to localstorage, is there any harm debouncing the final write until things settle?

Here's an example of what I mean:


      setState: (key, state, storage) => {
        // debounce storing state
        if (persistTimer != null) {
          clearTimeout(persistTimer);
        }
        persistTimer = setTimeout(() => {
          storage.setItem(key, JSON.stringify(state));
          persistTimer = null;
        }, 100);
      },
robinvdvleuten commented 3 years ago

If writing to LocalStorage becomes a performance bottleneck, I would suggest revisiting your application architecture. I would definitely not recommend doing async writes to prevent state mismatch issues.