6

Since cookies are server-side and Localstorage is client-side, which is the fastest for the user to retrieve? (ref. Local Storage vs Cookies)

I assume if the client's machine is slow, then cookies are faster? Or that makes no diffrence?

I am using both localstorage & cookies for a project and both are retrieved using jQuery. That means, jquery has to get loaded, then the data is being retrieved.

How can I make this faster? I don't know how both work. For example, some say that Cookies are being retrieved once the HTML is trying to load before the style's and js files and others say when DOM is ready.

Does anyone know accurate details about which is faster for the user to retrieve?

Thank you

Community
  • 1
  • 1
jQuerybeast
  • 13,038
  • 35
  • 114
  • 189

2 Answers2

27

Since cookies are server-side and Localstorage is client-side, which is the fastest for the user to retrieve?

You're starting off with an incorrect assumption. Cookies are stored client-side as well. Both localStorage and cookies are stored client side.

The difference however, and that cookies can be set, manipulated from the server side. localStorage is only workable from the client side.

I assume if the client's machine is slow, then cookies are faster? Or that makes no diffrence [sic]?

They are such lightweight operations I wouldn't worry about speed.

Performance between the two is not a factor for choosing one. It's what are your needs.

Cookies:

  1. Have worked in browsers for a long time. Every modern, and not so modern, browser support cookies.
  2. Can be accessed and manipulated server side.
  3. Are typically limited to a few kilobytes, depending on browser.

localStorage:

  1. Is a relatively new concept. Not all browsers support it. IE 6, 7 for example, and you're out of luck.
  2. Is inaccessible from the server side. You could make an AJAX call back to the server with data that is stored in localStorage.
  3. Have a W3C recommendation of being able to store 5 MB.

How can I make this faster?

I don't think either one can directly introduce a performance problem. Other problems, like waiting for the DOM to be in ready state, waiting for script files to load; etc. can introduce slowdowns.

vcsjones
  • 128,004
  • 28
  • 283
  • 274
  • Thank you. I think I've learned something from here. I am working with a dashboard project and things are sorted with drag and drop. The order is saved, so I was thinking where it would be better to store them so that on return to retrieve the data faster... – jQuerybeast Oct 17 '11 at 21:37
  • @jQuerybeast reading from localStorage or cookies is so fast in browsers these days that you can do 1 or 1,000 reads from either one and not notice a difference. – vcsjones Oct 17 '11 at 21:41
  • 2
    a jsperf does exist comparing the two: http://jsperf.com/cookie-vs-localstorage but, at the same time, the difference between the two are very small if all you are making is a single set or get. you will only see a difference in the two if you are making thousands of requests to the two, which is very unlikely. Choose based on what you need, not speed. – Kevin B Oct 17 '11 at 21:46
1

Both cookies and LocalStorage are stored locally on the client. There should be no noticeable difference in how quickly you can read from them.

Cookies do have the disadvantage of being transmitted to and from the server on HTTP requests and responses. This may increase the page size, increasing page load time.

If you are using jQuery to read cookies/LocalStorage, then the browser will have to wait for the jQuery script to be downloaded if it isn't in the browser's cache.

Greg
  • 21,917
  • 11
  • 55
  • 77