Unlock the Power of Client-Side Storage with localStorage
What is localStorage?
The localStorage object is one of the two mechanisms of Web Storage, allowing developers to store data persistently on the client’s browser. This stored data can be accessed throughout a particular domain using easy-to-use API methods. The data remains even after the browser window is closed, making it a powerful tool for storing non-sensitive data such as user preferences, application states, and API responses.
Using sessionStorage vs. localStorage
While similar to localStorage, sessionStorage temporarily stores data for a specified tab and only persists through page reloads and restorations. When a webpage is loaded into a browser tab, sessionStorage creates a new page session and assigns it to that tab. This means that data stored in each kind of Web Storage is distinct for each protocol of a given page.
Key Differences Between sessionStorage and localStorage
- localStorage and sessionStorage work similarly, but the main difference lies in their persistence.
- Data stored in localStorage is shared between tabs of the same origin and lasts forever unless the browser’s storage is cleared or we clear localStorage using JavaScript or manually.
Web Storage vs. HTTP Cookies
HTTP cookies are a conventional mechanism for storing small bits of data exchanged between the client and the server during each HTTP request. However, they can be a privacy nightmare due to their ability to track user data. Web Storage, on the other hand, provides a more secure and private way to store data locally.
When to Use Cookies?
Cookies are best suited for session management and are one of the most widely supported solutions for doing so. However, they have limitations, such as being transmitted with every request made to the server, which makes them unfit for caching large values.
Storing JavaScript Objects in localStorage
Modern web apps often require saving JavaScript objects locally to provide offline access, restore application state, or cache API responses. To do so, we need to serialize the object data using JSON.stringify and then store it in localStorage. When retrieving the data, we can deserialize it using JSON.parse.
const data = { name: 'John', age: 30 };
localStorage.setItem('data', JSON.stringify(data));
const storedData = localStorage.getItem('data');
const parsedData = JSON.parse(storedData);
console.log(parsedData); // { name: 'John', age: 30 }
Storing Multiple Objects in localStorage
Let’s say we have multiple objects we want to store in localStorage. We can turn them into an object array and then serialize them as a single JSON string. Alternatively, we can store each object with separate keys, but accessing all of them quickly can be done using a namespace approach.
const data1 = { name: 'John', age: 30 };
const data2 = { name: 'Jane', age: 25 };
const dataArray = [data1, data2];
localStorage.setItem('data', JSON.stringify(dataArray));
// Using namespace approach
localStorage.setItem('undefined', JSON.stringify(data1));
localStorage.setItem('undefined', JSON.stringify(data2));
Limitations of Storing Objects in localStorage
localStorage has its limitations, including a storage size limit of 5-10MB per origin, depending on the browser. Additionally, Web Storage API operations are synchronous and block the main thread, which can impact performance.
Best Practices for Storing Objects in localStorage
When storing objects in localStorage, it’s essential to respect the size limit and avoid storing more than 3-4MB of data per origin. Additionally, we should be mindful of the data we store locally and take advantage of the JSON stringify and parse methods to serialize and deserialize object data.
By leveraging the power of localStorage, we can create more efficient, personalized, and offline-capable web applications that provide a better user experience.