Tuesday, 13 October 2020

Local Storage vs Session Storage vs Cookie

With Advent of Html5 , we have got various option to cache or store info on client browser. Previously we were having only cookies , which were very restrictive and size of cookies was very small. but now we local storage and session storage as well. and cookies has been talk of past , though it is getting used for various purposes. let's talk about all these

LocalStorage
localStorage is a way to store data on the client’s computer. It allows the saving of key/value pairs in a web browser and it stores data with no expiration date. localStorage can only be accessed via JavaScript, and HTML5. However, the user has the ability to clear the browser data/cache to erase all localStorage data.

//Set the value in a local storage object
localStorage.setItem('name'myName);

//Get the value from storage object
localStorage.getItem('name');

//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege

Pros:
  1. stores data with no expiration date
  2. storage limit is about 5MB
  3. data is never transferred to the server
Cons:
  1. plaintext, hence not secure by design
  2. limited to string data, hence need to be serialized
  3. can only be read on client-side
Session storage
  1. Stores data only for a session, meaning that the data is stored until the browser (or tab) is closed
  2. data is never transferred to the server
  3. can only be read on client-side
  4. storage limit is about 5-10MB
  5. opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window
sessionStorage.setItem('key''value');
var data = sessionStorage.getItem('key');

Cookie
  1. Stores data that has to be sent back to the server with subsequent XHR requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side .
  2. Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side.
  3. Size must be less than 4KB.
  4. Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie.
Cookies can be updated , set using document.cookie object from browser window object.
document.cookie = "yummy_cookie=choco"
document.cookie = "tasty_cookie=strawberry"
console.log(document.cookie); 
// logs "yummy_cookie=choco; tasty_cookie=strawberry"



Reference: https://medium.com

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives