Persistent Storage in Browser

Understanding Persistent Storage

Imagine a digital diary where you jot down reminders. Some reminders, like daily tasks, can disappear after the day ends. Others, like long-term goals, should remain until you decide to delete them. Similarly, web applications use persistent storage to manage data on a user's browser.

While most data is stored in databases hosted on servers (like Google Maps storing road data), not all data needs to be fetched from or saved to a central server. Persistent storage on the browser provides a flexible, efficient way to manage stateful data.

By the end of this article, you will be able to:

Use Cases for Persistent Browser Storage

Persistent storage is especially useful for stateful data that is specific to an individual user. Here are some common scenarios:

Short-term data: Some data, like the state of a multi-step form, matters only during the current browser session. This data can be removed when the browser is closed.

Long-term data: Other data, such as user preferences or bookmarks, needs to persist even after the browser is reopened. In these cases, a longer-term storage strategy is more appropriate.

Approaches to Persistent Data in JavaScript

Persistent storage in the browser is typically implemented using either cookies or Web Storage. Let’s dive into each approach.

Cookies

Cookies are like small notes passed between the browser and server. They are used to store stateful data such as user preferences, login sessions, or browsing habits. Cookies are shared with the server on every HTTP request, making them ideal for scenarios where the backend needs access to the data.

Key features:


// Example of setting cookies
document.cookie = "favoriteColor=blue; expires=Fri, 31 Dec 2025 23:59:59 GMT";
document.cookie = "favoriteDance=tango; currentUserId=12345";

// Reading cookies
console.log(document.cookie); // Outputs: favoriteColor=blue; favoriteDance=tango; currentUserId=12345
            

Web Storage

Web Storage, which includes localStorage and sessionStorage, is another way to store stateful data. Unlike cookies, data stored in Web Storage is not sent to the server with HTTP requests. This makes it ideal for client-side use cases.

Key differences between localStorage and sessionStorage:


// Example of Web Storage
localStorage.setItem("theme", "dark"); // Data persists indefinitely
sessionStorage.setItem("currentPage", "home"); // Data persists until tab is closed

// Retrieve data
console.log(localStorage.getItem("theme")); // Outputs: dark
console.log(sessionStorage.getItem("currentPage")); // Outputs: home

// Remove data
localStorage.removeItem("theme");
sessionStorage.clear();
            

Note: Non-string data (e.g., arrays or objects) must be converted to JSON before storage using JSON.stringify and parsed back using JSON.parse.

Comparison of Persistent Storage Methods

Feature Cookies localStorage sessionStorage
Data Limit 4 KB 5-10 MB 5-10 MB
Scope Accessible by server and client Client-side only Client-side only
Persistence Until expiration (if set) Until cleared Until tab is closed

Key Takeaways

Mastering browser storage empowers you to create seamless, stateful user experiences.