cookie, localStorage, sessionStorage
Comparison
Name | Description | Size | Life |
---|---|---|---|
Cookie | Small piece of text data filed by server and saved in browser and carried in every request. Mostly used for identifying user. | 4KB | User defined expiration. Default cleared on browser close. |
localStorage | Permanent storage for each domain. Not used in request. | 5MB | Permanent, unless cleared. |
sessionStorage | Transient storage for each domain. Not used in request. | 5MB | Available in current session. Cleared when tab or browser closed/refreshed. |
Use case
- Cookie
- Identifying user. Mostly through some encrypted token.
- Personalization. Like settings or themes.
- Tracking. Track user behavior.
- LocalStorage
- Stores bigger data like data generated by web games.
- SessionStorage
- A big form divided into several small ones.
Caveats
- Either cookies, localStorage or sessionStorage are susceptible to security vulnerability like XSS forgeries. Be careful not to save sensitive data with them.
cookie, localStorage, sessionStorage
https://blog-cdt1.vercel.app/2022/10/31/cookie-localStorage-sessionStorage/