Best practice of using localstorage to store a large number of objects

I am currently experimenting with localStorage to store a large number of objects of the same type, and I'm a little confused.

One way of thinking is to save the entire object in an array. But then for each read / write of one object, I need to deserialize / serialize the entire array.

Another way is to directly save each object using its key in localStorage. This will facilitate access to each object, but I'm worried about the number of objects that will be stored (tens of thousands). In addition, to retrieve all the objects, an iteration of the entire local storage is required!

I wonder which path will be better in your experience? Also, would it be wise to use a more sophisticated client database such as PouchDB?

+5
source share
3 answers

If you do not want to have many keys, you can:

  • JSON strings with a string with \n and save them as one key
  • Create and update indexes stored under separate keys, each of which associates a key with a specific row number.

In this case, parsing strings is simply .split('\n') , which is ~ 2 orders of magnitude faster, then JSON.parse .

Please note that you may need a special effort to synchronize open tabs at the same time. This can be a problem in difficult cases.

localStorage has both good and bad details.

Good details:

  • Synchronous;
  • very fast, both read and write - this is only memcpy - this is the bandwidth of 100 + Mbps even on weak devices (for example, JSON.stringify usually 5-20 times slower than localStorage.setItem );
  • thoroughly tested and reliable.

Bad news:

  • There are no transactions, so you need engineering effort to sync tabs;
  • I think that you have no more than 2 MB (because there are systems with this limit);
  • 2Mb of storage actually means 1M of characters that you can save.

These points show the applicability limits of localStorage as a database. LS is good for tasks where you need synchronization and speed, and where you can trim your DB to fit into the quota.

So localStorage is good for caches and logs. Not more.

+1
source

If you need something simple to store a large number of keys / values, and you don't want to worry about types, then I recommend LocalForage . You can store strings, numbers, arrays, objects, blobs, whatever you want. It uses IndexedDB and WebSQL where they are available, so storage limits are significantly higher than LocalStorage.

PouchDB also works, but the API is more complex, and it is better suited for synchronizing data with CouchDB on the server.

+2
source

I personally have not used localStorage to manage many items.

However, the template that I usually use for data management is to load the complete information database into a javascript object, manage it in memory during the process, and save it to localStorage again when the process is complete.

Of course, this template may not be a good approach to your needs, depending on your project specifications.

If you need to keep data permanently, data access can be a problem, and probably using small access to a small database is the best option.

If your data volume is exceptionally high, this can also be a problem for managing it in memory, however, depending on the data model, you can create it for efficient structures that allow you to load and save data just when you need it.

+1
source

All Articles