会话存储相对于本地存储有什么好处吗?

发布于 2024-10-30 00:02:44 字数 53 浏览 7 评论 0原文

除了非持久性和仅限于当前窗口之外,会话存储相对于本地存储还有什么好处(性能、数据访问等)吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

樱娆 2024-11-06 00:02:44

localStoragesessionStorage 都扩展了 存储。除了 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.

浸婚纱 2024-11-06 00:02:44

唯一的区别是它们的过期时间不同。

  • 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 over localStorage 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
山色无中 2024-11-06 00:02:44

其他几点可能有助于理解本地存储和会话存储之间的差异

  1. 本地存储和会话存储的范围都仅限于文档来源,因此

    https://mydomain.example/ 
    http://mydomain.example/
    https://mydomain.example:8080/
    

    以上所有 URL不会共享相同的存储空间。 (注意网页路径不影响网页存储)

  2. 即使在不同选项卡中打开同源策略的文档,会话存储也是不同的,因此同一网页在两个不同选项卡中打开不能共享相同的会话存储。

  3. 本地和会话存储也受浏览器供应商的限制。浏览器供应商。因此 IE 保存的存储数据无法被 Chrome 或 FF 读取。

Few other points which might be helpful to understand differences between local and session storage

  1. 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)

  2. 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.

  3. Both local and session storage are also scoped by browser vendors. So storage data saved by IE cannot be read by Chrome or FF.

离去的眼神 2024-11-06 00:02:44

localStoragesessionStorage 之间的主要区别在于 sessionStorage 每个选项卡都是唯一的。如果关闭该选项卡,sessionStorage 将被删除,而 localStorage 不会。另外,您无法在选项卡之间进行通信:)

另一个细微的区别是,例如在 Safari (8.0.3) 上,localStorage 有 2551 k 个字符的限制,但 sessionStorage无限存储

在 Chrome (v43) 上,localStoragesessionStorage 均限制为 5101 k 个字符(正常/隐身模式之间没有区别)

在 Firefox 上,localStoragesessionStorage 限制为 5120 k 个字符(正常/私人模式之间没有区别)

速度上没有任何区别:)

Mobile Safari 和 Mobile Chrome 也存在问题,私人模式 Safari & Chrome 的最大空间为 0KB

The main difference between localStorage and sessionStorage is that sessionStorage is unique per tab. If you close the tab the sessionStorage 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 but sessionStorage has unlimited storage

On Chrome (v43) both localStorage and sessionStorage are limited to 5101 k characters (no difference between normal / incognito mode)

On Firefox both localStorage and sessionStorage 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

风轻花落早 2024-11-06 00:02:44

sessionStoragelocalStorage 相同,只不过它只存储一个会话的数据,并且当用户关闭创建它的浏览器窗口时它将被删除。

sessionStorage is the same as localStorage, 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.

回首观望 2024-11-06 00:02:44
  • sessionStorage 为每个给定源维护一个单独的存储区域,该区域在页面会话期间可用(只要浏览器打开,包括页面重新加载和恢复)

  • localStorage 执行相同的操作,但即使关闭并重新打开浏览器也会持续存在。

我从 Web Storage API

  • sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long as the browser is open, including page reloads and restores)

  • localStorage does the same thing, but persists even when the browser is closed and reopened.

I took this from Web Storage API

救赎№ 2024-11-06 00:02:44

性能方面,我的(粗略)测量发现 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

拧巴小姐 2024-11-06 00:02:44

会话存储和本地存储在行为上是相同的,除了本地存储将存储数据,直到并且除非用户删除缓存和 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.

网名女生简单气质 2024-11-06 00:02:44

在我看来,会话存储相对于本地存储的优势在于它具有 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.)

年华零落成诗 2024-11-06 00:02:44

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

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

一旦浏览器的选项卡(会话)关闭,该选项卡上的会话存储将被清除,而在本地存储的情况下,我们需要明确清除它。最大存储限制分别为 5MB10MB

我们可以保存和检索数据,如下所示,

保存:

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();

PS: < code>getItem() 也会以字符串形式返回数据,如果它是对象,我们需要将其转换JSON 格式才能访问。

您可以在此处阅读有关浏览器存储的更多信息。

  1. localStorage、sessionStorage 和 cookie 之间的区别

  2. 本地存储与会话存储

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 and 10MB.

We can save and retrieve the data like below,

To Save:

sessionStorage.setItem('id', noOfClicks);   // localStorage.setItem('id', noOfClicks);

sessionStorage.setItem('userDetails', JSON.stringify(userDetails));   // if it's object

To Get:

sessionStorage.getItem('id');    // localStorage.getItem('id');

User user = JSON.parse(sessionStorage.getItem("userDetails")) as User;  // if it's object

To Modify:

sessionStorage.removeItem('id');    // localStorage.removeItem('id');

sessionStorage.clear();   // localStorage.clear();

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..

  1. Difference between localStorage, sessionStorage and cookies

  2. localstorage-vs-sessionstorage

安穩 2024-11-06 00:02:44

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

//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

会话存储:它与本地存储日期相同,只是当网络用户关闭浏览器窗口时它将删除所有窗口。

//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";

了解更多点击

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.

//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

Session Storage: It is same like local storage date except it will delete all windows when browser windows closed by a web user.

//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";

Read More Click

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文