I have several items that I’m storing in the local storage. What the page loads, I have about 20 lines like this:
var SomeVar = localStorage.getItem('SomeKey');
I’d like to change this and get all the storage data in one read, store it in a temporary object, and get the 20 variables from that temporary object.
How can I read local storage in one read?
Thanks.
I can’t think of a reason you’d want to do this, but you could take advantage of the fact that
localStorageis basically an object and do something like this:I’m looping through the values in
localStorageand adding them to a temporary local storage variable. Then you can access the temporary local storage variable with the same keys as you would standardlocalStorage, but with standard object property syntax, not the customgetItem()syntax thatlocalStorageuses.Edit: Here’s a suggestion. Save all your variables to one object, then load and save that object from and to
localStorageinstead of saving seperate objects. It’s the individual variable accessing calls that take the most time. It takes roughly the same amount of time to read 10 characters from a single variable into memory that it does reading 1000, according to this post onlocalStorageoptimization.