DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. The Beginner’s Guide to Web Storage API and Related Tools

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

In this article, I we go over how to best use web storage APIs and some of the libraries and tools related to web storage.

Swathi Prasad user avatar by
Swathi Prasad
·
May. 04, 17 · Tutorial
Like (6)
Save
Tweet
Share
15.06K Views

Join the DZone community and get the full member experience.

Join For Free

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. Web storage offers a 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 or 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 a 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. Note that 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 the storage 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 all the 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 improve the way we work with web storage APIs. 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

It 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 JSON API Adapter with some relationship sugar added.

Source Code

vue-ls

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

Source Code

react-native-storage

It 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 a 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)

API Web Service Data (computing) JavaScript library

Published at DZone with permission of Swathi Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mr. Over, the Engineer [Comic]
  • Microservices Discovery With Eureka
  • Express Hibernate Queries as Type-Safe Java Streams
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: