Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving app state with localstorage

Just a quick question about saving an apps state using local storage. I'm about to start work on an iOS web app and I'm wondering if there may be any advantages or disadvantage to either of these models. Also, is there any major performance hit to saving every tiny change of the app state into local storage?

Number 1

Save the entire app state object as an JSON string to a single local storage key value pair.

var appstate = {
    string: 'string of text',
    somebool: true,
    someint: 16
}
localStorage.setItem('appState', JSON.stringify(appstate));

Number 2

Save each variable of the app state to it's own key value pair in local storage.

var appstate = {
    string: 'string of text',
    somebool: true,
    someint: 16
}
localStorage.setItem('string', appstate.string);
localStorage.setItem('bool', appstate.somebool);
localStorage.setItem('int', appstate.someint);
like image 358
Phil Avatar asked Apr 27 '26 02:04

Phil


1 Answers

The only reason I would think it could be more efficient to store values separately would be if you anticipate values changing independently of each other. If they are stored separately you could refresh one set of values without touching another, and therefore have better handling of value expiration. If, for example, somebool changes frequently but the rest does not, it might be better to store it separately. Group together data with similar expiration and volatility.

Otherwise, if you just want to save the entire application state, I would think that a single string would be fine.

like image 167
nullability Avatar answered Apr 28 '26 15:04

nullability