localStorage vs sessionStorage vs cookies

AngularjsHtmlCookiesLocal StorageSession Cookies

Angularjs Problem Overview


I am working in an app where I need to keep some data during the user is logged in and I have that question, what is the difference among localStorage, sessionStorage, cookies ???

I was asking what can I use in order to persist some data in the DOM, even if the user refresh the page, some people says: use sessionStorage, or localStorage, then, someone came up with the idea of use ngCookies because it works in every browser, but, which should I use ?

Angularjs Solutions


Solution 1 - Angularjs

localStorage and sessionStorage are both so-called WebStorages and features of HTML5.

localStorage stores information as long as the user does not delete them.

sessionStorage stores information as long as the session goes. Usually until the user closes the tab/browser.

cookies are simply cookies, which are supported by older browsers and usually are a fallback for frameworks that use the above mentioned WebStorages.

In contrast cookies can store way less information then WebStorages and the information in WebStorages is never transferred to the server.

Keep in mind that the EU has a regulation that requires websites to inform their users about the usage of cookies. I dont know whether this also applies to WebStorages

Solution 2 - Angularjs

sessionStorage object: The sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed. it is not available when a file is run locally.

Data stored in the sessionStorage object is accessible only from the page that initially stored the data; so this doesn't meet your requirement

localStorage object: Data stored using the localStorage object is persisted until it is specifically removed via JavaScript or the user clears the browser’s cache.

Data stored in the localStorage object is accessible only from the domain that initially stored the data.

For your case, I think you make consider about using cookie or session, pls. note cookie has 4K size limitation per server.

Solution 3 - Angularjs

Addition to other answers, WebStorages cannot access subdomain and/or parent domain.

Solution 4 - Angularjs

LocalStorage - Stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser cache / Locally Stored Data. Storage limit is the maximum amongst the three

SessionStorage - The sessionStorage object stores data only for a session, meaning that the data is stored until the browser (or tab) is closed. Data is never transferred to the server. Storage limit is larger than a cookie (at least 5MB).

Cookie - Stores data that has to be sent back to the server with subsequent requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side (normally from server-side). Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side. Size must be less than 4KB. Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie.

Solution 5 - Angularjs

localStorage :

  1. Data Limit : 5 MB
  2. Data sent for every http request : no

sessionStorage :

  1. Data Limit : 5 MB
  2. Data sent for every http request : no
  3. Data will be cleared once window or tab is closed

I would say, use localstorage/sessionStorage if data is non sensitive else use cookies

Solution 6 - Angularjs

Cookies are just hold 4kbs data and as expiry time.

localStorage are permanent cookies holds 4 MB data it will delete when user clears cache

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNonView Question on Stackoverflow
Solution 1 - AngularjsJonathanView Answer on Stackoverflow
Solution 2 - AngularjsJackie DongView Answer on Stackoverflow
Solution 3 - AngularjsAliView Answer on Stackoverflow
Solution 4 - AngularjsAnushView Answer on Stackoverflow
Solution 5 - AngularjsPrashant BiradarView Answer on Stackoverflow
Solution 6 - AngularjsShivappaView Answer on Stackoverflow