HTML5 Local storage vs. Session storage

后端 未结 10 2169
甜味超标
甜味超标 2020-11-22 03:16

Apart from being non persistent and scoped only to the current window, are there any benefits (performance, data access, etc) to Session Storage over Local Storage?

相关标签:
10条回答
  • 2020-11-22 03:44

    The advantage of the session storage over local storage, in my opinion, is that it has unlimited capacity in Firefox, and won't persist longer than the session. (Of course it depends on what your goal is.)

    0 讨论(0)
  • 2020-11-22 03:47

    The main difference between localStorage and sessionStorage is that sessionStorage is unique per tab. If you close the tab the sessionStorage gets deleted, localStorage does not. Also you cannot communicate between tabs :)

    Another subtle difference is that for example on Safari (8.0.3) localStorage has a limit of 2551 k characters but sessionStorage has unlimited storage

    On Chrome (v43) both localStorage and sessionStorage are limited to 5101 k characters (no difference between normal / incognito mode)

    On Firefox both localStorage and sessionStorage are limited to 5120 k characters (no difference between normal / private mode )

    No difference in speed whatsoever :)

    There's also a problem with Mobile Safari and Mobile Chrome, Private Mode Safari & Chrome have a maximum space of 0KB

    0 讨论(0)
  • 2020-11-22 03:50

    performance wise, my (crude) measurements found no difference on 1000 writes and reads

    security wise, intuitively it would seem the localStore might be shut down before the sessionStore, but have no concrete evidence - maybe someone else does?

    functional wise, concur with digitalFresh above

    0 讨论(0)
  • 2020-11-22 03:50
    • sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long as the browser is open, including page reloads and restores)

    • localStorage does the same thing, but persists even when the browser is closed and reopened.

    I took this from https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

    0 讨论(0)
  • 2020-11-22 03:51

    Ya session storage and local storage are same in behaviour except one that is local storage will store the data until and unless the user delete the cache and cookies and session storage data will retain in the system until we close the session i,e until we close the session storage created window.

    0 讨论(0)
  • 2020-11-22 03:56

    Few other points which might be helpful to understand differences between local and session storage

    1. Both local storage and session storage are scoped to document origin, so

      https://mydomain.com/
      http://mydomain.com/
      https://mydomain.com:8080/

      All of the above URL's will not share the same storage. (Notice path of the web page does not affect the web storage)

    2. Session storage is different even for the document with same origin policy open in different tabs, so same web page open in two different tabs cannot share the same session storage.

    3. Both local and session storage are also scoped by browser vendors. So storage data saved by IE cannot be read by Chrome or FF.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题