in a website, setting one cookie is better than setting many single cookies?

倖福魔咒の 提交于 2020-01-03 05:23:06

问题


Suppose that you have to set 12 cookies, would it be better to store all of their values in a single "big" cookie or store each value on its own cookie? I want to know if that improves website speed.

Thanks.


回答1:


I think the maximium size of a cookie is about 4k. If you need to store more data than that, then you're better off using several cookies.

i don't think there will be a big performance issue using multiple cookies. in my opinion, it may be more efficient to store each piece of data in its own cookie rather than parsing through a bogger cookie for each "crum" of data each time you need it.

Hope this helps!




回答2:


The number of cookies is hardly going to affect speed in any meaningful way since either they'll all be sent through the request separated by Cookie: headers, or you'll have one Cookie: header with data concatenated in your own way. Technically, the "faster" way would be to use one cookie.

In reality, this is a micro-optimization and you shouldn't worry about it.




回答3:


If there is any doubt the best thing to do is try it both ways and use a free utility like tcptrace to show you how much bandwidth each method consumes. That should definitively give you your answer, because if there is a difference in performance between the two methods you've proposed, it is probably due to bandwidth consumption.




回答4:


You could always use sessions, which store the session ID in a cookie and store everything else in your database/filesystem referenced against that session ID. That way, the data is not sent on every request, only the session ID is.



来源:https://stackoverflow.com/questions/1105951/in-a-website-setting-one-cookie-is-better-than-setting-many-single-cookies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!