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

Related Posts:

  • ES6 Modules ModulesJavaScript modules allow you to break up your code into separate files. This makes it easier to maintain the code-base.ES Modules rely on… Read More
  • ES6 Spread OperatorThe ES6 (ECMAScript 2015) spread operator is a powerful feature in JavaScript that allows you to expand or spread elements from an array or object int… Read More
  • ES6 DestructuringDestructuring in ES6 (ECMAScript 2015) is a feature that allows you to unpack values from arrays or properties from objects into distinct variables. I… Read More
  • HTML5 Local and Session Storage HTML5 Local StorageLocal Storage is a web storage feature that allows you to store key-value pairs in a web browser. It’s part of the Web Storage API… Read More
  • The difference between == and === in jQuery/JavaScript In this post I would like to explain the difference between double (==) and triple (===) equals in JavaScript/jQuery. A quick and short explanation … Read More

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# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) 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