除了非持久化和仅作用于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能,数据访问等)?
当前回答
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 sessionStorageover 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—用于长期使用。 sessionStorage—当您需要存储更改的东西或临时的东西时使用
其他回答
localStorage和sessionStorage之间的主要区别是sessionStorage每个选项卡都是唯一的。如果关闭选项卡,sessionStorage将被删除,而localStorage不会。你也不能在标签之间通信:)
另一个微妙的区别是,例如在Safari(8.0.3)上,localStorage限制为2551 k字符,而sessionStorage有无限的存储空间
在Chrome (v43)中,localStorage和sessionStorage都被限制在5101 k个字符(正常/隐身模式没有区别)
在Firefox上,localStorage和sessionStorage都被限制为5120 k字符(正常模式和私有模式之间没有区别)
速度没有任何差异:)
移动Safari和移动Chrome也有一个问题,私人模式Safari和Chrome的最大空间为0KB
sessionStorage与localStorage相同,只是它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时,它将被删除。
晚回答,但觉得在这里补充了一些观点。
会话存储将可用于特定的选项卡,因为我们可以通过浏览器使用本地存储。两者默认都是同源的,我们也可以手动用键、值对存储值(值必须是字符串)。
一旦浏览器的选项卡(会话)关闭,那么会话存储将在该选项卡上被清除,在本地存储的情况下,我们需要显式地清除它。最大存储限制分别为5MB和10MB。
我们可以像下面这样保存和检索数据,
保存:
sessionStorage.setItem('id', noOfClicks); // localStorage.setItem('id', noOfClicks);
sessionStorage.setItem('userDetails', JSON.stringify(userDetails)); // if it's object
得到:
sessionStorage.getItem('id'); // localStorage.getItem('id');
User user = JSON.parse(sessionStorage.getItem("userDetails")) as User; // if it's object
修改:
sessionStorage.removeItem('id'); // localStorage.removeItem('id');
sessionStorage.clear(); // localStorage.clear();
p.s.: getItem()也返回数据作为字符串,我们需要将其转换为JSON格式来访问如果它是对象。
你可以阅读更多关于浏览器存储在这里。
localStorage, sessionStorage和cookie的区别 localstorage-vs-sessionstorage
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 sessionStorageover 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—用于长期使用。 sessionStorage—当您需要存储更改的东西或临时的东西时使用
其他几点可能有助于理解本地存储和会话存储之间的区别
Both local storage and session storage are scoped to document origin, so https://mydomain.example/ http://mydomain.example/ https://mydomain.example: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) 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.
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?