ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
cookie最大的缺陷是在每一次HTTP请求中都会携带所有符合规则的cookie数据.这会[增加请求响应时间](http://yuiblog.com/blog/2007/03/01/performance-research-part-3/),特别是XHR请求. 在HTML5中使用`sessionStorage`和`localStorage`代替cookie是更好的做法. 这另种方法可以将数据永久或者以session时间存储在用户本地.数据不会随着HTTP请求传递.所以我们优先使用web storage,仅仅使用cookie作为替代方案. ~~~ // if localStorage is present, use that if (('localStorage' in window) && window.localStorage !== null) { // easy object property API localStorage.wishlist = '["unicorn", "Narwhal", "deathbear"]'; } else { // without sessionStorage we'll have to use a far-future cookie // with document.cookie's awkward API var date = new Date(); date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); var expires = date.toGMTString(); var cookiestr = 'wishlist=["unicorn", "Narwhal", "deathbear"];' + ' expires=' + expires + '; path=/'; document.cookie = cookiestr; } ~~~