本地存储vs Cookie [英] Local Storage vs Cookies

查看:244
本文介绍了本地存储vs Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将所有Cookie移动到本地存储中来减少我的网站上的加载时间,因为它们似乎具有相同的功能。在使用本地存储替换cookie功能时,除了显而易见的兼容性问题,是否有任何优点/缺点(特别是性能方面)?

I want to reduce load times on my websites by moving all cookies into local storage since they seem to have the same functionality. Are there any pros/cons (especially performance-wise) in using local storage to replace cookie functionality except for the obvious compatibility issues?

推荐答案

p> Cookie和本地存储用途不同。 Cookie主要用于阅读服务器端,本地存储空间只能在客户端中阅读。所以问题是,在你的应用程序,谁需要这个数据—客户端或服务器?

Cookies and local storage serve different purposes. Cookies are primarily for reading server-side, local storage can only be read client-side. So the question is, in your app, who needs this data — the client or the server?

如果是你的客户端(你的JavaScript),那么通过一切手段切换。您通过发送每个HTTP头中的所有数据浪费带宽。

If it's your client (your JavaScript), then by all means switch. You're wasting bandwidth by sending all the data in each HTTP header.

如果它是您的服务器,本地存储不那么有用,因为您必须转发数据(以Ajax或隐藏的表单字段或某物)。如果服务器只需要每个请求的总数据的一小部分,那么这可能没问题。

If it's your server, local storage isn't so useful because you'd have to forward the data along somehow (with Ajax or hidden form fields or something). This might be okay if the server only needs a small subset of the total data for each request.

You'll want to leave your session cookie as a cookie either way though.

根据技术差异,以及我的理解:

As per the technical difference, and also my understanding:


  1. 除了是保存数据的旧方式之外,Cookie还提供了 4096 个字节(实际上为4095)的限制 - 。本地存储空间大小每个域5MB - 问题 也提及它

  1. Apart from being an old way of saving data, Cookies give you a limit of 4096 bytes (4095, actually) - its per cookie. Local Storage is as big as 5MB per domain - SO Question also mentions it

localStorage c $ c>存储接口。它存储无过期日期的数据,并且通过JavaScript仅清除 ,或清除浏览器缓存/本地存储的数据 - 与Cookie过期不同。

localStorage is an implementation of the Storage Interface. It stores data with no expiration date, and gets cleared only through JavaScript, or clearing the Browser Cache / Locally Stored Data - unlike cookie expiry.

这篇关于本地存储vs Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆