除了非持久化和仅作用于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能,数据访问等)?
当前回答
sessionStorage为每个给定的源维护一个单独的存储区域,该存储区域在页面会话期间可用(只要浏览器打开,包括页面重新加载和恢复)。 localStorage做同样的事情,但是即使浏览器关闭并重新打开,它仍然存在。
我从Web存储API中获得了这个
其他回答
本地存储:保存没有过期日期的用户信息数据,当用户关闭浏览器窗口时,这些数据不会被删除,它将在天,周,月和年可用。
//Set the value in a local storage object
localStorage.setItem('name', myName);
//Get the value from storage object
localStorage.getItem('name');
//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege
会话存储:它与本地存储日期相同,只是当web用户关闭浏览器窗口时,它会删除所有窗口。
//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";
阅读更多点击
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—当您需要存储更改的东西或临时的东西时使用
sessionStorage与localStorage相同,只是它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时,它将被删除。
性能方面,我的(粗略的)测量在1000次写入和读取时没有发现差异
安全方面,从直觉上看,localStore可能会在sessionStore之前关闭,但没有具体的证据-也许其他人会?
功能方面,与digitalFresh一致
晚回答,但觉得在这里补充了一些观点。
会话存储将可用于特定的选项卡,因为我们可以通过浏览器使用本地存储。两者默认都是同源的,我们也可以手动用键、值对存储值(值必须是字符串)。
一旦浏览器的选项卡(会话)关闭,那么会话存储将在该选项卡上被清除,在本地存储的情况下,我们需要显式地清除它。最大存储限制分别为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
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?