The Beginner’s Guide to Web Storage API and Related Tools

Web Storage is a software method and protocol used for storing data in a web browser. It is similar to HTTP session cookies, for storing name-value pairs on the client side, but with a greatly enhanced capacity. The web storage offers storage capacity between 2MB and 10 MB depending on the browser. Check the detailed list of all the storage capacities for various browsers here.

In this article, I will give you a brief overview of web storage API and we will go through some of the libraries and tools related to web storage.

localStorage and sessionStorage

There are two kinds of storage areas within web storage:

  • sessionStorage that maintains a separate storage area for each given origin that is available for the duration of the page session.
  • localStorage is similar to sessionStorage, but persists even when the browser is closed and reopened. It comes in handy for transferring data between windows or tabs, if that’s required.

These storage areas are available via the window.sessionStorage and window.localStorage properties.

Storing and Reading Data

Data is added to storage through the method setItem(). It takes key-value pair as arguments. The key must be of type string, but the type of value may vary.

For example:

let localStorage = window.localStorage;

localStorage.setItem(“key1”, “value1”);

let sessionStorage = window.sessionStorage;

sessionStorage.setItem(“key2”, “value2”);

Alternatively, the data can be also set as follows. But it is recommended to use the web storage API ( setItem() ) to prevent the drawbacks associated with using plain objects as key-value stores.

localStorage["key1"] = “value1”;

sessionStorage["key2"] = “value2”;

localStorage.key1 = “value1”;

sessionStorage.key2 = “value2”;

Data can be read from storage via getItem().

let string1 = localStorage.getItem("key1");

let string2 = sessionStorage.getItem("key2");

We can also iterate over stored data as follows:

for (let i = 0; i < localStorage.length; i++) {

let key = localStorage.key(i);

let value = localStorage.getItem(key); 

// do something 

}

Removing Stored Data

When we no longer need stored data, we must explicitly remove them especially for localStorage, as it persists even if the browser is closed. To remove individual key-value pairs, we can do as follows:

localStorage.removeItem("key1");

sessionStorage.removeItem("key2");

To remove the entire data at once, we can use clear().

localStorage.clear();

sessionStorage.clear();

JavaScript Libraries and Tools for Web Storage 

There are several libraries and tools out there, which improves the way we work with web storage API. Let’s look at some of them.

Proxy Storage

This JavaScript library manages an adapter that implements an interface similar to Web Storage to normalize the API for document.cookie, window.localStorage and window.sessionStorage.

Source Code

webStorage

This JavaScript library improves the way you work with localStorage or sessionStorage.

Source code

ngx-webstorage

This library provides an easy to use service to manage the web storages (local and session) for your ng2 application. It also provides two decorators to synchronize the component attributes and the web storages.

Source Code

h5webstorage

It is a web storage library for Angular 2 applications.

Source Code

Ember localStorage

This addon provides a storageFor computed property that returns a proxy and persists the changes to localStorage or sessionStorage. It ships with an ember-data adapter that works almost the same as the JSONAPIAdapter with some relationship sugar added.

Source Code

vue-ls

This plugin is for working with local storage from Vue context.

Source Code

react-native-storage

This is a local storage wrapper for both react-native(AsyncStorage) and browser(localStorage).

Source Code

Wrapping Up

Web storage does well while storing insensitive data on the client or data which do not need frequent refresh. It’s more of a balancing act wherein the ease of client side storage is purchased at the cost of performance. Keep things as light as possible. If you wish to explore further, here are some references.

Web Storage API

Web Storage (Second edition)

 

One thought on “The Beginner’s Guide to Web Storage API and Related Tools

Comments are closed.