Local Storage vs. Session Storage vs. Cookies (2024)

In this post, I’ll talk about how cookies compare to browser storage. You’ll understand why cookies came out, the problem they solve, and their limitations.

Then you’ll explore what browser storage is, and dive deeper into local storage and session storage. You’ll look at their features, where they can be useful, and how to use them via JavaScript.

You’ll then see how you can contrast the features, advantages, and disadvantages of each and also highlight specific use cases of each.

You’ll also look at the best practices or approach to keep in mind while using each of them and the best place to store your JWT or auth tokens.

How many times have you seen a popup on a website that says:

This website is using cookies to store your information.

I'm guessing so often that you've lost the count!

Cookies have been used since time immemorial for storing session related information of a user. This allows websites to provide a unique and engaging experience to their users.

However, cookies can be a bit difficult to use, have limited use-case, and have small data storing capacity. To combat this, modern browsers come with their own storage mechanisms like local storage and session storage.

In this post, I’ll talk about these storage mechanisms. You'll understand how local storage, session storage, and cookies compare against one another and explore common use cases of each. By the end of this post, you'll know exactly when to use which.

A Brief History of Cookies

Let's have a refresher on the history of cookies.

Back in the day, websites used HTTP protocol which is stateless. This meant that they couldn't store user-related information like the user’s session id, the name of the user, etc., in the browser.

As the web advanced and grew more popular, websites started storing user related information in the browser itself. This helped them differentiate what kind of user is interacting with their website and provide a personalized experience to the user.

Enter Cookies

That's how cookies were born. They presented a mechanism to store lightweight client or server side data on the browser as key value pairs. They also had an expiry timestamp after which they were automatically deleted by the browser.

Also, both browser and the server could set and retrieve cookies from a user’s browser. Moreover, these cookies were sent alongside each HTTP request automatically. This came in handy for server side websites at a time when single page applications weren't a thing. They could easily discern a user's information with each HTTP request that user made.

The Rise of Browser Storage

Where cookies solved a great problem, it had some limitations. First, they could only store data up to a few kbs. As client-side applications became more complex, there was a need to store more complex data in the browser.

With the onset of HTML5, websites were introduced to browser storage APIs for storing client side information. These APIs were available on browser's window objects globally. Thus, any JavaScript running in the browser could easily access them. The major differentiator was that they had higher data storage capacity and could only be accessed by client-side JavaScript.

Browsers rolled out two storage mechanisms — local storage and session storage. So let's explore and understand them in detail.

Browser’s Session Storage

Let's first take a peek at where the session storage resides in the browser:

  1. Open the developer tools in the browser and head over to the "Application" tab.

    Local Storage vs. Session Storage vs. Cookies (1)

  2. Under the storage section, you'll find a section named "Session Storage".

    Local Storage vs. Session Storage vs. Cookies (2)

  3. Click on it, and you'll be able to see the session storage for that website.

There it is! Now, let's look at the features of browser's session storage.

Storage Capacity

Session storage can store data ranging between 5mb - 10mb. The exact amount of storage capacity varies as per each browser's implementation, but it's a lot more than 4kb of storage capacity cookies offer!

Data Persistence

As the name suggests, session storage only persists the data as long as a browser tab is opened. This means that each time you open a new tab or a new browser window, a new session storage is created. So any data you store inside the session storage will automatically get deleted when you close that tab/window.

Using Session Storage

You can access the session storage in the window object:

window.sessionStorage//Storage {length: 0}

This would return the length of the session storage along with an object representing the data that's currently present inside. Since it's empty to begin with, the length is 0. Note that you may directly access the sessionStorage object as well.

Adding Data

Let's add a key-value pair to the session storage using the setItem function available in the sessionStorage object:

sessionStorage.setItem("id", "123")

This will set a new item in the session storage with the key id and value 123. If you simply invoke the sessionStorage object now:

sessionStorage//Storage{id: '123', length: 1}

You now get your recently added data back!

You'll also see this inside the session storage section of the browser's application tab:

Local Storage vs. Session Storage vs. Cookies (3)

Inserting Complex JSON Data

Let's add a bit more complex JSON object in the session storage that looks like this:

const data = { _id: "61c6c1df7cda7d370a9ef601", index: 0, guid: "13672f0e-f693-4704-a6f9-839ff36e8960", isActive: true, balance: "$3,602.49", picture: "http://placehold.it/32x32", age: 25, friends: [ { id: 0, name: "Adkins Coleman", }, { id: 1, name: "Howe Douglas", }, { id: 2, name: "Becky Velez", }, ],}

You'll first need to stringify it since the value against a key can only be a string. Then, you'll store it inside the session storage using the setItem method:

sessionStorage.setItem("user_data", JSON.stringify(data))

It should now appear inside the session storage of the browser's application tab:

Local Storage vs. Session Storage vs. Cookies (4)

Awesome! Let's take a look at retrieving this data.

Retrieving Data

You can use the getItem() function to retrieve a value stored against a key from the session storage by specifying the key as the first parameter in the function.

sessionStorage.getItem("id")//'123'

If you're retrieving an object, you'll need to use JSON.parse first to convert the string into a JavaScript object:

JSON.parse(sessionStorage.getItem("user_data"))

Usecase

Since data in session storage persists only across a browser tab, there are some unique usecases for session storage.

Booking Applications

Session storage can be used in multi-step processes that must be performed in a single instance. This includes booking flights, hotels, movie tickets, train reservations etc. You can store the details of the previous steps in the browser's session storage to prepopulate those forms or inputs.

Since these are critical activities that must be performed in a single go, the data will automatically get deleted when the user jumps to a new tab or a new browser window.

Prompting Login/Signups to a User

Blogging websites, newsletters, tutorial websites etc., have tons of visitors who read through the content without creating an account. In such scenarios, you can subtly prompt the user every time they visit the website to create an account. You can track the session of each user in the session storage.

Every time a user reads a blog post or an article on a different tab, you can ask them to create an account. This could be a great way to offer a non-blocking experience for your users whilst effectively converting them to a signed-up user for your website.

Browser’s Local Storage

Under the application tab where session storage resides, you'll find a section called local storage right underneath it.

Local Storage vs. Session Storage vs. Cookies (5)

That's where you can see everything you store inside the local storage of your browser. Local storage works, appears, and similar to session storage. For instance, just like Session storage, local storage can also store data ranging between 5mb - 10mb depending upon a browser's implementation.

Data Persistence

Unlike session storage where data is automatically deleted when a browser tab or window is closed, data in local storage has no default expiry. It's only deleted if you manually delete that data from the local storage either directly, via browser settings, or through your JavaScript code.

That means that data in a local storage persists even after a particular tab or browser window is closed.

Using Local Storage

You can add and retrieve data from local storage in the same way you perform those operations with session storage. The only change is now you'll have to use the localStorage object to perform these operations instead.

For instance, you can add an item to the localStorage, as follows:

localStorage.setItem("id", "123")

Consequently, you can retrieve an item using the getItem() function:

localStorage.getItem("id")//'123'

Usecase

Local storage has a number of uses due to the fact that data inside it has no default expiry. Think about all those scenarios where you want to store some global data that's accessed often in your application.

Generic

For instance, your user's email id, username, full-name, etc. All these data are widely used throughout different pages of your application. Storing this data inside the local storage will help you prevent unwanted API calls to the server to fetch this data. Also, since this data isn't normally changed often, the chances of having inconsistent data across the browser and the server is quite low.

Application Specific

You can also use it to store application specific data that is immutable throughout a login session of a user. For instance, if you have an ecommerce site, you can store the preferred payment option of the user, default delivery addresses, etc. You can also store user preferences such as the theme a user prefers for your website (dark or light mode).

Cookies vs. Browser Storage

Now that you've understand how browser storage works, you can compare them effectively against cookies. However, let's first look at their similarities.

Similarities

Remember in the beginning I asked you how many times you've seen that cookies popup? Most of these popups also have an option where you can choose not to accept these cookies.

In other words, cookies are blockable by users and so is browser storage. Users can choose not to allow their data to be stored via any of these mechanisms — and hence, your application should never completely rely on these storage mechanisms.

Both cookies and browser storage store key-value pairs as strings and are well supported in all modern browsers. The data inside this can also be easily edited by the user.

Differences

You know that browser storage has a greater storage capacity than cookies. Hence, browser storage is always a better choice than cookies to store large client-side data.

However, since session storage and local storage have different data persistence, you should carefully choose either of them depending on how long you want the data to persist.

Cookies allow you to automatically set a TTL or expiry time; are transferred with every HTTP request to the server; and, can also be accessed by the server. These features are missing in browser storage. Which brings us to the question — where would cookies be actually more useful than browser storage?

Local Storage vs. Session Storage vs. Cookies (6)

The answer is server side data! If you've dealt with authentication before, you know that at some point you need to store the authentication token or JWT of a user somewhere where it's easily accessible. That's where cookies are helpful. But why can't we use or why shouldn't we use browser storage here?

Storing Your JWT/Auth Token

Data such as JWT or Auth token should not be stored in browser storage because they can be accessed by any client side JavaScript running in the browser. This means that if your application somehow leaves an XSS vulnerability, your user's authentication token could be easily leaked to the attacker.

The attacker could then make false requests, modify your user's data in the database, and do a lot of damage for your application as well as users. Hence, it's always best to store JWTs in http only cookies. Http only cookies are special cookies that cannot be accessed by client side JavaScript. This way they're secure against XSS attacks.

Also, authentication token is often refreshed on expiry and one can use cookies TTL easily to manage that. For simpler cases, one can also store JWT inside regular cookies by setting a TTL.

But all in all, authentication itself can be a tricky subject. If you're looking for a no-code identity platform that can seamlessly handle authentication for your application, consider using LoginRadius.

Wrapping it Up

Now that you understand how powerful browser storage is, don't feel shy to use it in your applications. Use cookies for server side data where you need a TTL, session storage for specific use cases discussed above, and local storage to manage global data in your application.

However, avoid the pattern where your single page application directly interacts with the local storage. For instance, if you're using React with Redux to manage the state in your application, let your reducers take control over what goes and comes out of local storage. Your React components should be abstracted from using local storage directly.

Finally, since local storage data has no default expiry, be vary of when you're clearing this data to avoid data inconsistency between your frontend and backend.

Local Storage vs. Session Storage vs. Cookies (2024)

FAQs

Local Storage vs. Session Storage vs. Cookies? ›

To be more specific, local Storage stores up to 10 megabytes and session storage stores up to 5 megabytes. On the other hand, these provide a very restrictive and small storage capacity of 4 kilobytes. So we cannot store large amounts of information in cookies.

When to use local storage vs session storage vs cookies? ›

Which is better local storage or session storage? If we want some data on the browser, we often utilize the local storage object. Cookies are used if we want them on the server, and session storage is used if we want to delete the data if the user closes that particular tab or the season.

What are the correct options that differentiates between local storage vs cookies? ›

Local storage is a modern client-side storage method that is part of the HTML5 specification. It allows you to store up to 5 MB of data in key-value pairs and, unlike cookies, it does not send data back to the server with each request.

Is local storage more secure than cookies? ›

Security Considerations: Cookies are susceptible to security vulnerabilities such as XSS and CSRF attacks due to their automatic inclusion in HTTP requests. Local storage provides better security since data stored locally is not automatically transmitted to the server, reducing the risk of unauthorized access.

Which is better local storage or cache? ›

Local storage is for persistently storing small user-specific data, ensuring longevity across visits. Caching, on the other hand, optimizes performance by temporarily storing frequently accessed resources to speed up page loading.

When not to use session storage? ›

However, similar to local storage, session storage is susceptible to XSS attacks. Attackers can steal your tokens from the storage and use them to impersonate users. As such, it's also not advisable to store session tokens and other sensitive data in session storage.

What happens when localStorage is full? ›

When you try to store data in localStorage, the browser checks whether there's enough remaining space for the current domain. If yes: The data is stored, overwriting values if an identical key already exists.

Does local storage expire? ›

localStorage is similar to sessionStorage , except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.

What is the difference between a cookie and a session? ›

The key difference between sessions and cookies is that sessions are saved on the server side while cookies are saved on the client side. Cookies are small text files used to store user information on the user's computer. Sessions are used to store user information on the user's server side.

What is the main difference between cache and cookies? ›

A cache is used to store website and application content to enhance user accessibility. Cookies are used by websites and applications to store user activity and identify their preferences.

What are 3 disadvantages of local storage? ›

Disadvantages of local storage
  • Higher costs: Upfront investment and ongoing maintenance of physical storage can be expensive.
  • Limited scalability: Enhancing storage capacity usually requires more hardware and physical space.
Apr 9, 2024

Does blocking cookies block local storage? ›

If user consent is granted, local storage and session storage are placed on a user's device and are shown in cookie declaration. If user consent is rejected, local storage and session storage remain blocked.

Why shouldn't you use localStorage? ›

Synchronous Blocking Operations: localStorage is not asynchronous and will block the main thread. It may even make your animations choppy depending on how much you read/ write and at what frequency. Asynchronicity is fundamental to creating fluid applications, especially for mobile devices.

When to use session storage vs local storage? ›

For most cases, we use the local Storage object if we want some data to be on the browser. If we want it on the server, then we use it, and the session storage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.

What is the difference between cookie storage and local storage? ›

The two have different purposes, and hence different strengths and weaknesses. Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.

Why is local storage better? ›

Because the data resides on a physical device, such as a computer or on-premises server, the user has more direct control over data privacy and security. Local storage also normally offers faster data access compared to cloud storage. Latency is reduced because data retrieval happens directly from a device.

When should you use local storage? ›

For most cases, we use the local Storage object if we want some data to be on the browser. If we want it on the server, then we use it, and the session storage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.

When to use IndexedDB vs localStorage? ›

In summary when you compare IndexedDB vs localStorage, IndexedDB will win at any case where much data is handled while localStorage has better performance on small key-value datasets.

What is the difference between a session and a cookie? ›

The key difference between sessions and cookies is that sessions are saved on the server side while cookies are saved on the client side. Cookies are small text files used to store user information on the user's computer. Sessions are used to store user information on the user's server side.

What should session storage be used for? ›

Session storage can be used in multi-step processes that must be performed in a single instance. This includes booking flights, hotels, movie tickets, train reservations etc.

Top Articles
How Dave McCormick profited off the 2008 financial collapse
Convert Plian to US Dollar (PI to USD) - BeInCrypto
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 5561

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.