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


当前回答

本地存储:保存没有过期日期的用户信息数据,当用户关闭浏览器窗口时,这些数据不会被删除,它将在天,周,月和年可用。

//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—当您需要存储更改的东西或临时的东西时使用

性能方面,我的(粗略的)测量在1000次写入和读取时没有发现差异

安全方面,从直觉上看,localStore可能会在sessionStore之前关闭,但没有具体的证据-也许其他人会?

功能方面,与digitalFresh一致

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相同,只是它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时,它将被删除。

会话存储和本地存储在行为上是相同的,除了一个是本地存储将存储数据,直到用户删除缓存和cookie,会话存储数据将保留在系统中,直到我们关闭会话i,直到我们关闭会话存储创建的窗口。