tags:

views:

427

answers:

3

I have a social network type site (member site) I store a lot of user data like username, userID number, email, first name, photo URL, into a user session when the user logs in to cut down on the ammount of DB queries per page. I sometimes hear people talk about using cookies for some things, I realize cookies should never be used for username/passwords but mu main question is, does a cookie slow down the page loading time? On a user's homepage I have blocks that a user can drag and drop to re-arrange there homepage, my first version I stored the location of the blocks in a cookie, I now store this value in mysql and I build the locations of the block in php on page load instead of using javascript to set the initial locations. When I first used cookies to set there locations though the page would load and the blocks would be in there original location, the page would then really fast change the location of these blocks from the value it got from the cookie, however this was much much slower and made the page seem really slow to load. I have always thought cookies were slow since then but maybe it wasn't the cookie that slowed it down.

So do cookies slow a page down?

+1  A: 

Cookies will slightly increase the load time for your pages, as the cookie data has to be sent with each request.

For the HTML page, this won't make much difference, but if you have all your assets on the same domain (as is usual) it can add up to a significant difference.

Actually reading the cookie in PHP should not take a noticeable amount of time.

Greg
would a subdomain count as being on the same domain? I was planning on putting images and stuff on images.domain.com
jasondavis
If your main website is like http://example.com yes, if your main site is like http://www.example.com then no (though I believe you can share cookies between subdomains if you want to)
Greg
@Greg - Did you mean www.example.com for the second domain?
Nirmal
+2  A: 

A cookie is just an extra HTTP header with key/value pairs. Parsing that header and filling the $_COOKIE array does not add significant overhead (it's almost certainly not going to be a bottleneck).

If you're storing a lot of data in the cookie (i.e., not just a session id),then it's worth remembering that this data gets sent with every request to your domain, not just for PHP pages but for images, CSS, JS etc. For this reason, in a high traffic site you might arrange for these "static" elements to be served from a different domain to lessen the effect of this overhead.

If that cookie value is a session identifier, then PHP needs to retrieve that session, either from the filesystem, database or other storage mechanism to populate the $_SESSION array. This can take add a little time, but it really depends on the mechanism used.

Paul Dixon
+1  A: 

If I understand correctly, you are saying your page was slow when :

  • the positions of the blocks were stored in cookies
  • and the blocks were positionned in Javascript on page load

I think the "slow" part doesn't come from the cookies :

  • those are quite small, compared to the size of the page
    • yes, it adds some overhead, but not that important for a single page
    • it becomes more important if you have lots of assets (images, static files, ...), as they are sent with each request, though
  • they don't contain much data


But, with that solution, what could cause an impression of slowness is that :

  • the page is loaded with block at their default positions
  • some JS code is executed to re-position the blocks
    • which means waiting a bit before blocks are re-positionned
    • which takes some processing time to execute the JS code
    • which also means, for the browser, re-drawing the page several times (each time something changes).


IMHO, what makes your page "faster" now is that the client receives the whole page with blocks already well positionned -- which means no processing at all on page load :-)

The smaller/absent cookies might make a small difference... but small.


If you want to read more about cookies size and their impact on load-times, and some recommandations, you can read this article from Yahoo : Performance Research, Part 3: When the Cookie Crumbles ; it's a pretty interesting read, and not too hard to understand.

Pascal MARTIN