DEV Community

Cover image for Session Storage vs Local Storage vs Cookies
Kyle Foo
Kyle Foo

Posted on • Edited on

8 3

Session Storage vs Local Storage vs Cookies

Wondering which browser storages best fit your use case? Here's a brief comparison to help you with making decision:

Session Storage Local Storage Cookies
Storage Size 5MB on browser 10MB on browser 4kb on browser or server
HTML support HTML 5 HTML 5 HTML 4&5
Accessibility Own tab only Across tab & windows Across tab & windows
Expiry Expires on tab close No expiration *Configurable
Updatable via *Web Browser API *Web Browser API Server Side Request or *Web Browser API

*Configurable Expiry: When a server responds to a browser request, it can send down a Set-Cookie header with one or many cookies, ex:
Set-Cookie: user_id=5; Expires=Fri, 5 Oct 2025 14:28:00 GMT; Secure; SameSite=Strict; HttpOnly

The HttpOnly Attribute
Cookies with the HttpOnly attribute are only accessible via HTTP(S). They are not accessible via JavaScript. Prevent XSS vulnerability.

The Secure Attribute
Cookies with the Secure attribute are only sent over an encrypted connection (HTTPS). It helps to reduce the potential damage of man-in-the-middle (MITM) attacks.

The SameSite Attribute
The SameSite attribute restricts the origin from which the cookie will be sent. It helps to prevent CSRF attacks.

*Web Browser API: see https://developer.mozilla.org/en-US/docs/Web/API for list of APIs

Reference: https://martinthoma.medium.com/cookies-742318b821c5

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (3)

Collapse
 
w6 profile image
w6

Great table, I will definitely bookmark this!

Collapse
 
rexyandrian profile image
Rexy Andriansyah

Which one the most secure on browser storages ?

Collapse
 
kylefoo profile image
Kyle Foo • Edited

don't think any of them provide security since all of them on the client side, unless u encrypt your data before storing them, have your application (server-side?) to decrypt every time you wanna access them.