除了非持久化和仅作用于当前窗口之外,会话存储相对于本地存储是否有任何好处(性能,数据访问等)?


当前回答

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

其他回答

晚回答,但觉得在这里补充了一些观点。

会话存储将可用于特定的选项卡,因为我们可以通过浏览器使用本地存储。两者默认都是同源的,我们也可以手动用键、值对存储值(值必须是字符串)。

一旦浏览器的选项卡(会话)关闭,那么会话存储将在该选项卡上被清除,在本地存储的情况下,我们需要显式地清除它。最大存储限制分别为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

在我看来,会话存储相对于本地存储的优势在于,它在Firefox中具有无限的容量,并且不会持续比会话更长的时间。(当然这取决于你的目标是什么。)

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一致