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?
For most cases, we use the localStorage object if we want some data to be on the browser. If we want it on the server, then we use cookies, and the sessionStorage is used when we want to destroy the data whenever that specific tab gets closed or the season is closed by the user.
HTML5 introduces two mechanisms, similar to HTTP session cookies, for storing structured data on the client side and to overcome following drawbacks. Cookies are included with every HTTP request, thereby slowing down your web application by transmitting the same data.
Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance. Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.
localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage
.
That is, the data stored in localStorage
persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
For sessionStorage
, changes are only available per tab. Changes made are saved and available for the current page in that tab until it is closed. Once it is closed, the stored data is deleted.
The only difference is that localStorage has a different expiration time, sessionStorage
will only be accessible while and by the window that created it is open. localStorage
lasts until you delete it or the user deletes it.
Lets say that you wanted to save a login username and password you would want to use sessionStorage
over localStorage
for security reasons (ie. another person accessing their account at a later time).
But if you wanted to save a user's settings on their machine you would probably want localStorage
. All in all:
localStorage
- use for long term use.sessionStorage
- use when you need to store somthing that changes or somthing temporary
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With