会话存储相对于本地存储有什么好处吗?
除了非持久性和仅限于当前窗口之外,会话存储相对于本地存储还有什么好处(性能、数据访问等)吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
localStorage 和 sessionStorage 都扩展了 存储。除了
sessionStorage
的预期“非持久性”之外,它们之间没有任何区别。也就是说,存储在
localStorage
中的数据将一直保留到明确删除为止。所做的更改将被保存并可供当前和将来对该站点的所有访问使用。对于
sessionStorage
,更改仅适用于每个选项卡。所做的更改将被保存并可用于该选项卡中的当前页面,直到关闭为止。一旦关闭,存储的数据将被删除。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.唯一的区别是它们的过期时间不同。
sessionStorage
只能在创建它的窗口打开时进行访问。localStorage
将持续存在,直到您删除它或用户删除它为止。假设您想要保存登录用户名和密码,出于安全原因(即其他人稍后访问其帐户),您希望使用
sessionStorage
而不是localStorage
。但如果您想将用户的设置保存在他们的计算机上,您可能需要
localStorage
。总之
localStorage
- 用于长期使用。sessionStorage
- 当您需要存储更改的内容或临时的内容时使用The only difference is that they have different expiration times.
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.Let's say that you wanted to save a login username and password you would want to use
sessionStorage
overlocalStorage
for security reasons (i.e. 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
.In conclusion
localStorage
- use for long term use.sessionStorage
- use when you need to store something that changes or something temporary其他几点可能有助于理解本地存储和会话存储之间的差异
本地存储和会话存储的范围都仅限于文档来源,因此
以上所有 URL不会共享相同的存储空间。 (注意网页路径不影响网页存储)
即使在不同选项卡中打开同源策略的文档,会话存储也是不同的,因此同一网页在两个不同选项卡中打开不能共享相同的会话存储。
本地和会话存储也受浏览器供应商的限制。浏览器供应商。因此 IE 保存的存储数据无法被 Chrome 或 FF 读取。
Few other points which might be helpful to understand differences between local and session storage
Both local storage and session storage are scoped to document origin, so
All of the above URL's will not share the same storage. (Notice path of the web page does not affect the web storage)
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.
Both local and session storage are also scoped by browser vendors. So storage data saved by IE cannot be read by Chrome or FF.
localStorage
和sessionStorage
之间的主要区别在于sessionStorage
每个选项卡都是唯一的。如果关闭该选项卡,sessionStorage
将被删除,而localStorage
不会。另外,您无法在选项卡之间进行通信:)另一个细微的区别是,例如在 Safari (8.0.3) 上,
localStorage
有 2551 k 个字符的限制,但sessionStorage
有 无限存储在 Chrome (v43) 上,
localStorage
和sessionStorage
均限制为 5101 k 个字符(正常/隐身模式之间没有区别)在 Firefox 上,
localStorage
和sessionStorage
限制为 5120 k 个字符(正常/私人模式之间没有区别)速度上没有任何区别:)
Mobile Safari 和 Mobile Chrome 也存在问题,私人模式 Safari & Chrome 的最大空间为 0KB
The main difference between
localStorage
andsessionStorage
is thatsessionStorage
is unique per tab. If you close the tab thesessionStorage
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 butsessionStorage
has unlimited storageOn Chrome (v43) both
localStorage
andsessionStorage
are limited to 5101 k characters (no difference between normal / incognito mode)On Firefox both
localStorage
andsessionStorage
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
sessionStorage
与localStorage
相同,只不过它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时它将被删除。sessionStorage
is the same aslocalStorage
, except that it stores the data for only one session, and it will be removed when the user closes the browser window that created it.我从
Web Storage API
I took this from
Web Storage API
性能方面,我的(粗略)测量发现 1000 次写入和读取没有差异。
安全方面,直观上似乎 localStore 可能会在 sessionStore 之前关闭,但没有具体证据 - 也许其他人这样做?
功能方面,同意上面的 digitalFresh
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
会话存储和本地存储在行为上是相同的,除了本地存储将存储数据,直到并且除非用户删除缓存和 cookie,并且会话存储数据将保留在系统中,直到我们关闭会话,即直到我们关闭会话存储创建的窗口。
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.
在我看来,会话存储相对于本地存储的优势在于它具有 Firefox 中的容量不受限制,并且不会持续超过会话时间。 (当然这取决于你的目标是什么。)
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.)
回答晚了,但觉得在这里补充一些观点。
会话存储将可用于特定选项卡,因为我们可以通过浏览器使用本地存储。两者都默认为同源,我们也可以使用键、值对手动存储值(值必须是字符串)。
一旦浏览器的选项卡(会话)关闭,该选项卡上的会话存储将被清除,而在本地存储的情况下,我们需要明确清除它。最大存储限制分别为
5MB
和10MB
。我们可以保存和检索数据,如下所示,
保存:
获取:
修改:
PS: < code>getItem() 也会以字符串形式返回数据,如果它是对象,我们需要将其转换为 JSON 格式才能访问。
您可以在此处阅读有关浏览器存储的更多信息。
localStorage、sessionStorage 和 cookie 之间的区别
本地存储与会话存储
Late answer but felt to add some points here.
Session storage will be available for specific tab where as we can use Local storage through out the browser. Both are default to same origin and we can also store values manually with key, value pairs (value must be string).
Once tab (session) of the browser is closed then Session storage will be cleared on that tab, where as in case of Local storage we need to clear it explicitly. Maximum storage limit respectively
5MB
and10MB
.We can save and retrieve the data like below,
To Save:
To Get:
To Modify:
P.S:
getItem()
also return back the data as string and we need convert it into JSON format to access if it's object.You can read more about Browser Storages here..
Difference between localStorage, sessionStorage and cookies
localstorage-vs-sessionstorage
本地存储:它保留存储用户信息数据,没有过期日期,当用户关闭浏览器窗口时,该数据不会被删除,它将在日、周、月和年内可用。
会话存储:它与本地存储日期相同,只是当网络用户关闭浏览器窗口时它将删除所有窗口。
了解更多点击
Local storage: It keeps store the user information data without expiration date this data will not be deleted when user closed the browser windows it will be available for day, week, month and year.
Session Storage: It is same like local storage date except it will delete all windows when browser windows closed by a web user.
Read More Click