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.

Save State Performance

See original GitHub issue

Hey, I’m have come across a minor performance issue when saving state in the subscriber. In this instance there is a simple fix available too, but I thought I would reach out to see what people think about it first.

So, you’re wanting to persist the data, without getting in the way of your code performance. Then why are we blocking the saveState waiting for the write of data to the data storage in question.

The problem

The current implementation causes waits for localStorage or any other storage method to block the current thread waiting until its performed its operation.

This can cause the application to become non-responsive for a moment while we’re writing this state to disk.

/* helper to simulate the delay in localStorage writing */
function wait(ms){
   var start = new Date().getTime();
   var end = start;
   while(end < start + ms) {
     end = new Date().getTime();
  }
}

/* ORIGINAL blocking version */
const myPlugin = store => {
  store.subscribe((mutation, state) => {
    console.time('VuexSubscription')
    wait(7000)
    console.timeEnd('VuexSubscription')
  })
}

The proposal

Make the write method asynchronous. In this example we go one more and use requestIdleCallback in order to find some guaranteed free time and avoid jank.

/* PROPOSED non blocking version */
// Shim `requestIdleCallback`
const requestIdleCallback = window.requestIdleCallback || (cb => {
  let start = Date.now();
  return setTimeout(() => {
    cb({
      didTimeout: false,
      timeRemaining() {
        return Math.max(0, 50 - (Date.now() - start))
      }
    })
  }, 1);
})

const myPlugin = store => {
  store.subscribe((mutation, state) => {
    console.time('VuexSubscription')
    requestIdleCallback(() => {
      wait(7000)
    }, {timeout: 60}) 
    console.timeEnd('VuexSubscription')
  })
}

So, what do you think?

Is it absolutely important to block the main thread waiting for the data to write to the storage?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
kylewelsbycommented, Dec 11, 2017

For those coming from Google and looking for a quick patch solution.

Put this into your store/index.js

const requestIdleCallback = window.requestIdleCallback || (cb => {
  let start = Date.now()
  return setTimeout(() => {
    let data = {
      didTimeout: false,
      timeRemaining () {
        return Math.max(0, 50 - (Date.now() - start))
      }
    }
    cb(data)
  }, 1)
})

const vuePersistl = new VuexPersistence({
  // ... your other config
  saveState: (key, state, storage) => {
    requestIdleCallback(() => {
      let data = JSON.stringify(state)
      if (storage && storage._config && storage._config.name === 'localforage') {
        data = merge({}, state)
      }
      storage.setItem(key, data)
    })
  }
})

👍 have a great day

1reaction
kylewelsbycommented, Sep 24, 2018

Hey @rulrok the merge is deepmerge or lodash.merge

Read more comments on GitHub >

github_iconTop Results From Across the Web

Performance of save/restore VM state on an SSD
My PC has 16 GB RAM, of which 8 are assigned to the VM. Restoring the state takes ~15 seconds. This is in...
Read more >
Saved State module for ViewModel
As mentioned in Saving UI States, ViewModel objects can handle configuration changes, so you don't need to worry about state in rotations or...
Read more >
What is the difference between pause and save state in ...
If I save state, it seems like it's running state (it's memory) is written to disk such that it can be restored later....
Read more >
Restricted State Save Performance Data Collection CL
This document includes CL programs to automate the performance data collection of JobWatcher and Collection Services while performing a save ...
Read more >
How often to save player's state in persistent online games?
Save after every command from each player and autonomous thing (e.g. NPC) · Constant backup. · Impact to server performance; even if it's...
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